标签:
> 1.简单的存储字符串,状态,数据类型
// 存储数据
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// 字符串
- (void)setObject:(id)value forKey:(NSString *)defaultName;
( [defaults setObject:self.accountField.text forKey:MJAccountKey]; )
// 开关状态
- (void)setBool:(BOOL)value forKey:(NSString *)defaultName;
[defaults synchronize];
// 读取数据
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// 字符串
self.accountField.text = [defaults objectForKey:MJAccountKey];
// 开关状态
- (BOOL)boolForKey:(NSString *)defaultName;
> 2.对象存储到文件中的数组中
// 文件路径
#define MJContactsFilepath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"contacts.data"]
// 从文件中读取数组
contacts = [NSKeyedUnarchiver unarchiveObjectWithFile:MJContactsFilepath]
// 归档,将数组存放到文件中
[NSKeyedArchiver archiveRootObject:self.contacts toFile:MJContactsFilepath];
标签:
原文地址:http://www.cnblogs.com/shen5214444887/p/4811093.html