如果想要实现归档和反归档的操作需要签订一个协议 NSCoding
[NSKeyedArchiver archiveRootObject:stuArr toFile:documentPath];归档(写入)
- (void)encodeWithCoder:(NSCoder *)aCoder { // 归档
姓名"];
性别"];
爱好"];
年龄"];
}
NSArray *studentArray = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];反归档(读取)
- (id)initWithCoder:(NSCoder *)aDecoder { // 反归档
self = [super init];
if (self) {
姓名"];
年龄"];
爱好"];
性别"];
}
return self;
}
存储与读取的两个button 的绑定方法
- (IBAction)save:(UIButton *)button {
唐启哲" age:19];
NSString *rootPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *allPath = [rootPath stringByAppendingPathComponent:@"person.data"];
[NSKeyedArchiver archiveRootObject:person toFile:allPath];
}
- (IBAction)read:(UIButton *)button {
NSString *rootPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)[0];
NSString *allPath = [rootPath stringByAppendingPathComponent:@"person.data"];(若是person.plist文件 可加载一个数组)
MSPersonModel *person = [NSKeyedUnarchiver unarchiveObjectWithFile:allPath];
NSLog(@"%@ ---- %li", person.name, person.age);
}