I want to store a UIColor object as an attribute on a Core Data entity, then retrieve it and use it in the same fashion that I do the other attributes on that entity.
<
Another option available, Swift 3
extension UIColor {
class func color(withData data:Data) -> UIColor {
return NSKeyedUnarchiver.unarchiveObject(with: data) as! UIColor
}
func encode() -> Data {
return NSKeyedArchiver.archivedData(withRootObject: self)
}
}
Usage
var myColor = UIColor.green
// Encoding the color to data
let myColorData = myColor.encode() // This can be saved into coredata/UserDefaulrs
let newColor = UIColor.color(withData: myColorData) // Converting back to UIColor from Data