Saving UIColor to and loading from NSUserDefaults

Unreality picture Unreality · Aug 14, 2009 · Viewed 25.6k times · Source

What's the easiest way to save a UIColor into NSUserDefaults and then get it back out?

Answer

Wevah picture Wevah · Aug 14, 2009

One way of doing it might be to archive it (like with NSColor, though I haven't tested this):

NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];

And to get it back:

NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];