iOS NSUserDefaults的基本使用
2016-02-21 21:25 甘雨路 阅读(226) 评论(0) 编辑 收藏 举报/** * NSUserDefaults可以进行轻量级的数据存储 * NSUserDefaults支持的数据类型: NSString, NSNumber, NSDate, NSArray, * NSDictionary */ //初始化NSUserDefaults NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; //存储BOOL类型 [defaults setBool:YES forKey:@"fisrtLaund"]; //存储对象 [defaults setObject:@"LF" forKey:@"name"]; //存储Integer类型 [defaults setInteger:23 forKey:@"age"]; //同步到磁盘 [defaults synchronize]; //NSUserDefaults的读取 BOOL isFirst = [defaults boolForKey:@"fisrtLaund"]; id name = [defaults objectForKey:@"name"]; NSInteger age = [defaults integerForKey:@"age"]; NSLog(@"%d--%@--%ld",isFirst,name,age); //删除数据 [defaults removeObjectForKey:@"name"]; NSLog(@"%@",[defaults objectForKey:@"name"]);