Making UIColor Codable - conforming to protocol 'Encodable'

Why do you want UIColor additionally conform to Codable? It conforms already to NSSecureCoding so it's serializable to Data by default.

And even with your implementation you can encode a color

let encoded = UIColor.Words.adverb.encode()!

and decode it

let color = UIColor.color(data: encoded)

And particularly in Core Data you can use computed properties to transform a supported type to an unsupported and vice versa.

My recommedation for Core Data is to save the color as Int32 (the hex representation) or as (hex) String


This is an implementation I'm using in Core Data, an extension to convert the color to a hex string

extension UIColor {

    private func float2String(_ float : CGFloat) -> String {
        return String(format:"%02X", Int(round(float * 255)))
    }

    var hex : String {
        var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0
        getRed(&red, green: &green, blue: &blue, alpha: nil)
        return "#" + float2String(red) + float2String(green) + float2String(blue)
    }

    convenience init(hex : String) {
        let hex = hex.hasPrefix("#") ? String(hex.dropFirst()) : hex
        if hex.count == 6, hex.range(of: "[^0-9A-Fa-f]", options: .regularExpression) == nil {
            let chars = Array(hex)
            let numbers = stride(from: 0, to: chars.count, by: 2).map() {
                CGFloat(strtoul(String(chars[$0 ..< min($0 + 2, chars.count)]), nil, 16))
            }
            self.init(red: numbers[0] / 255, green: numbers[1] / 255, blue: numbers[2] / 255, alpha: 1.0)
        } else {
            self.init(white: 1.0, alpha: 1.0)
        }
    }
}

and the relevant part of the NSManagedObject subclass

@NSManaged public var hexColor: String


var color : UIColor {
    get { return UIColor(hex: hexColor) }
    set { hexColor = newValue.hex }
}