We all love NSUserDefaults for saving vital app info from session to session, but sometimes it can be tedious remembering the exact syntax. Here are two methods I found in a an open source framework that I think are pretty spiffy.

 


-(id)retrieveFromUserDefaults:(NSString *)key {
 NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

 if (standardUserDefaults) return [standardUserDefaults objectForKey:key];
 return nil;
}

-(void)saveToUserDefaults:(id)object key:(NSString *)key {
 NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

 if (standardUserDefaults) {
   [standardUserDefaults setObject:object forKey:key];
   [standardUserDefaults synchronize];
 }
}

There are no comments

Submit a Comment.