For Objective C
place like this.
1 2 | NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color]; [[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"]; |
To get back like this.
1 2 | NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"]; UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData]; |
For Swift
set like this
1 2 3 4 5 6 7 8 9 | var userSelectedColor : NSData? = (NSUserDefaults.standardUserDefaults().objectForKey("UserSelectedColor") as? NSData) if (userSelectedColor != nil) { var colorToSetAsDefault : UIColor = UIColor.redColor() var data : NSData = NSKeyedArchiver.archivedDataWithRootObject(colorToSetAsDefault) NSUserDefaults.standardUserDefaults().setObject(data, forKey: "UserSelectedColor") NSUserDefaults.standardUserDefaults().synchronize() println("SET DEFAULT USER COLOR TO RED") } |
get like this.
1 2 3 4 5 | if let userSelectedColorData = NSUserDefaults.standardUserDefaults().objectForKey("UserSelectedColor") as? NSData { if let userSelectedColor = NSKeyedUnarchiver.unarchiveObjectWithData(userSelectedColorData) as? UIColor { println(userSelectedColor) } } |
from:http://stackoverflow.com/questions/26464342/how-to-save-a-color-in-nsuserdefaults-in-swift