Created
July 28, 2014 08:34
-
-
Save nvkiet/e100d157917521ba3901 to your computer and use it in GitHub Desktop.
Save class object use NSUserDefaults + Mental
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| + (id)objectForKey:(NSString *)key | |
| { | |
| if ([key length] == 0) { | |
| return nil; | |
| } | |
| NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key]; | |
| if (!data) { | |
| return nil; | |
| } | |
| return [NSKeyedUnarchiver unarchiveObjectWithData:data]; | |
| } | |
| + (void)setObject:(id)value forKey:(NSString *)key | |
| { | |
| if ([key length] == 0) { | |
| return; | |
| } | |
| NSData *data = [NSKeyedArchiver archivedDataWithRootObject:value]; | |
| if (!data) { | |
| return; | |
| } | |
| [[NSUserDefaults standardUserDefaults] setObject:data forKey:key]; | |
| [[NSUserDefaults standardUserDefaults] synchronize]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment