> 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];