CIVector Codable

March 5, 2019

For my current side project, I have to export CIFilter parameters to json, and I've been running into issues with various CoreImage types not conforming to Swift's Codable. CIColor works well with Codable out of the box, but CIVector (which represents an arbitrary length list of CGFloats) is trickier.

Thought I'd share a wrapper type which I use to encode and decode CIVector:

struct CIVectorCodableWrapper {
    let vector: CIVector

extension CIVectorCodableWrapper: Codable {
    public init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        var floats: [CGFloat] = []
        while !container.isAtEnd {
            floats.append(try container.decode(CGFloat.self))
        vector = CIVector(floats: floats)

    func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        for i in 0..<vector.count {
            try container.encode(vector.value(at: i))

CIVector is semantically an "unkeyed container" type, so the Codable implementation encodes the vector's floats to an unkeyed coding container (with JSONEncoder, for example, this becomes an array).

Picture of me with a corgi

Noah Gilmore

Hello! I'm Noah, a software developer based in the San Francisco bay area. I focus mainly on iOS, Apple platform development, and full stack web development.

  • 💻 I'm writing a macOS editor for Atlassian Confluence called Fluency
  • 📱 I wrote an app which lets you create transparent app icons called Transparent App Icons
  • 🧩 I made a puzzle game for iPhone and iPad called Trestle
  • 🎨 I wrote a CoreImage filter utility app for iOS developers called
  • 👋 Please feel free to reach out on Twitter