使用偏好设置我们不需要关心文件的名字进行键值对的存储
其实质就是对字典的封装 我们一定要记得同步不然有时候不能及时更新数据
//存数据
1 -(IBAction)save:(id)sender{ 2 NSUserDefaults *us =[NSUserDefaults standardUserDefaults]; 3 4 [us setObject:@"name" forKey:@"key"]; 5 [us setBool:YES forKey:@"isOn"]; 6 //保持同步 7 [us synchronize]; 8 NSString* homePath = NSHomeDirectory(); 9 NSLog(@"%@", homePath); 10 }
//取数据 11 -(IBAction)read:(id)sender{ 12 NSUserDefaults *us =[NSUserDefaults standardUserDefaults]; 13 NSString *name =[us objectForKey:@"key"]; 14 BOOL state =[us objectForKey:@"isOn"]; 15 NSLog(@"%@,%d",name,state); 16 }
原文:http://www.cnblogs.com/lxlmq412/p/4839736.html