Skip to content

Instantly share code, notes, and snippets.

@caoer
Created October 13, 2012 10:13
Show Gist options
  • Save caoer/3884047 to your computer and use it in GitHub Desktop.
Save caoer/3884047 to your computer and use it in GitHub Desktop.
Objective-C: Save object to NSUserDefault.m
- (MyCustomObject *)getObject {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *MyCustomObjectEncodedObject = [defaults objectForKey:kMyCustomObjectKEY];
MyCustomObject *MyCustomObject = (MyCustomObject *)[NSKeyedUnarchiver unarchiveObjectWithData: MyCustomObjectEncodedObject];
return MyCustomObject;
}
- (void)setMyCustomObject:(MyCustomObject *)MyCustomObject {
NSData *MyCustomObjectEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:MyCustomObject];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:MyCustomObjectEncodedObject forKey:kMyCustomObjectKEY];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment