Facing problems in converting SwiftUI Color to UIColor to save in user defaults

Viewed 191

I am trying to save a SwiftUI Color instance to user defaults. My understanding of the process is to first convert the Color instance to UIColor, then use NSKeyedArchiver to convert it to Data, which can be saved in user defaults. The process to read a value back would be the reverse of the above.

I tried this but was not getting correct results, so I wrote the following code, just to test the conversion from Color to UIColor and back:

let color = Color.orange
let uiColor = UIColor(color)
let convertedColor = Color(uiColor)
print(color == convertedColor)

The above code prints false, which means the two Color instances are not equal. Not sure why that is, since I have just converted the Color instance to UIColor and back. Is there some loss of information during the conversion? If yes, then how can a Color instance be saved to user defaults and correctly read back? Thanks.

0 Answers
Related