I am storing an UIColor
in NSUserDefaults
, and the problem that I am having is that a simple equality check fails (for my tests) when retrieving the color back. Can somebody explain why this fails?
UIColor *color = [UIColor colorWithRed: 0.922 green: 0.404 blue: 0.024 alpha: 1];
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];
NSData *storedColorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *restoredColor = [NSKeyedUnarchiver unarchiveObjectWithData:storedColorData];
if ([color isEqual:restoredColor]) {
NSLog(@"Same color");
} else {
NSLog(@"Not the same color");
}
The result will be "Not the same color". Quick looking in the debugger will show that the colors are identical (the RGB and alpha values).