본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

A better way to access NSUserDefaults

by 테크한스 2015. 12. 11.
반응형

written by http://www.geronimobile.com/a-better-way-to-access-nsuserdefaults/



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.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-(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];
 }
}


반응형