Let's say that my application state is retrieved into an object (so that all information specific to the application instance is contained in one object), and this object supports the nscoding protocol. How can I easily save it and load it when I exit / start my application?
My current code is as follows
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
State* state = (State*)[defaults objectForKey:@"State"];
if(state!=nil)
{
viewController.state = state;
}
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.viewController.state forKey:@"State"];
}
But when loading is always always zero ... so I assume that this is not the best example :)
It turns out that NSUserDefaults only supports property list objects, such as NSArray, NSData, etc ... not user objects unless you wrap it in nsdata p>
adrin source
share