How can i store UIColor in CoreData without loss on 64 bit? On 32 bit the correct UIColor is returned.
CoreData setup
- attribute type: Transformable
- NSManagedObject subclass property: @NSManaged var color: UIColor?
Before the color value is stored
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
output e.g. red on 64bit:
0.20000000000000018
output red on 32 bit
0.199999928
After the color is retrieved from CoreData
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
output e.g. red on 64bit:
0.20000000298023224
output red on 32 bit:
0.199999928
Resulting Problem
Color comparison, using == , fails on 64 bit, because the values slightly differ. On 32 bit everything is fine and color comparison succeeds.
Assign
NSKeyedArchiver.archivedDataWithRootObject(color)to a data variable and save that to your Core Data store instead.To read the data, just assign
NSKeyedUnarchiver.unarchiveObjectWithData(colorData)to a color variable.In case you're wondering how to compare floats anyways, you can always refer to this.