使用上很容易,如下所示:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:string forKey:@"Hello world"];
NSArray *array = @[@"123",@"456"];
[userDefaults setObject:array forKey:@"Array"];
BOOL isValid = YES;
[userDefaults setBool: isValid forKey:@"isValid"];
int number = 100;
[userDefaults setInteger:number forKey:@"number"];
要注意的是,設定好後只是單純的cache住,要存進硬碟要用,才真正儲存
[userDefaults synchronize];
取值很容易
[userDefaults stringForKey:@"Hello world"];
[userDefaults arrayForKey:@"Array"];
[userDefaults boolForKey:@"isValid"];
[userDefaults integerForKey:@"number"];
參考網址:
NSUserDefaults (plist) 筆記
返璞归真,忘掉NSUserDefaults
NSUserDefaults Class Reference
NSUserDefaults customize behavior to match a user’s preferences