close
123456789101112131415161718192021222324252627282930313233343536373839 |
// 定義名稱 define key
#define kIsActive @"isActive"
#define kUserName @"userName"
// 讀取資料
-(void)loadInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"Name : %@", [defaults stringForKey:kUserName]);
NSLog(@"isActive : %@", ([defaults boolForKey:kIsActive] ? @"YES" : @"NO"));
}
// 儲存資料
-(void)saveInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userName = @"Hank";
BOOL isActive = YES;
[defaults setObject:userName forKey:kUserName];
[defaults setBool:isActive forKey:kIsActive];
}
// 清除資料
-(void)clearInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kUserName];
[defaults removeObjectForKey:kIsActive];
}
// 執行
- (void)viewDidLoad
{
[super viewDidLoad];
[self saveInfo];
[self loadInfo];
[self clearInfo];
[self loadInfo];
}
|
參考: NSUserDefaults Class Reference
http://whhnote.blogspot.tw/2012/02/ios-nsuserdefaults.html
全站熱搜
留言列表