step1.
// 定義名稱 define key#define kIsActive @"isActive"#define kUserName @"userName"step2.// 讀取資料-(void)loadInfo {NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];NSLog(@"Name : %@", [defaults stringForKey:kUserName]);NSLog(@"isActive : %@", ([defaults boolForKey:kIsActive] ? @"YES" : @"NO"));}step3.// 儲存資料-(void)saveInfo {NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];NSString *userName = @"Hank";BOOL isActive = YES;[defaults setObject:userName forKey:kUserName];[defaults setBool:isActive forKey:kIsActive];}step4.// 清除資料-(void)clearInfo {NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];[defaults removeObjectForKey:kUserName];[defaults removeObjectForKey:kIsActive];}step5. 主程式// 執行- (void)viewDidLoad{[super viewDidLoad];[self saveInfo];[self loadInfo];[self clearInfo];[self loadInfo];}
0 意見:
張貼留言