归档 & 反归档
2015-08-25 18:15
288 查看
如果想要实现归档和反归档的操作需要签订一个协议 NSCoding
[NSKeyedArchiver archiveRootObject:stuArr toFile:documentPath]; // 归档(写入)
- (void)encodeWithCoder:(NSCoder
*)aCoder { // 归档
[aCoder encodeObject:self.name forKey:@"姓名"];
[aCoder encodeObject:self.sex forKey:@"性别"];
[aCoder encodeObject:self.hobby forKey:@"爱好"];
[aCoder encodeInteger:self.age forKey:@"年龄"];
}
NSArray *studentArray = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
// 反归档(读取)
- (id)initWithCoder:(NSCoder *)aDecoder { // 反归档
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"姓名"];
self.age = [aDecoder decodeIntegerForKey:@"年龄"];
self.hobby = [aDecoder decodeObjectForKey:@"爱好"];
self.sex = [aDecoder decodeObjectForKey:@"性别"];
}
return self;
}
存储与读取的两个button 的绑定方法
- (IBAction)save:(UIButton *)button {
MSPersonModel *person = [MSPersonModel personWithName:@"唐启哲" 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);
}
[NSKeyedArchiver archiveRootObject:stuArr toFile:documentPath]; // 归档(写入)
- (void)encodeWithCoder:(NSCoder
*)aCoder { // 归档
[aCoder encodeObject:self.name forKey:@"姓名"];
[aCoder encodeObject:self.sex forKey:@"性别"];
[aCoder encodeObject:self.hobby forKey:@"爱好"];
[aCoder encodeInteger:self.age forKey:@"年龄"];
}
NSArray *studentArray = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
// 反归档(读取)
- (id)initWithCoder:(NSCoder *)aDecoder { // 反归档
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"姓名"];
self.age = [aDecoder decodeIntegerForKey:@"年龄"];
self.hobby = [aDecoder decodeObjectForKey:@"爱好"];
self.sex = [aDecoder decodeObjectForKey:@"性别"];
}
return self;
}
存储与读取的两个button 的绑定方法
- (IBAction)save:(UIButton *)button {
MSPersonModel *person = [MSPersonModel personWithName:@"唐启哲" 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);
}
相关文章推荐
- 在服务器 IIS7.0上配置发布网站 遇到404 或401错误
- struts文件上传,获取文件名和文件类型
- 数据结构-线性表
- 什么是Bower
- 关于线程池ThreadPoolExecutor使用总结
- SAE微信公众号PHP SDK, token一直验证失败
- Dream------Hadoop--FSDataInputStream和FSDataOutputStream
- JS实现图片延迟加载
- android 获取网络状态
- 三种将公用配置文件读取为InputStream的方法
- UVa 10570:Meeting with Aliens(贪心)
- macox安装scrapy
- Unity3D官方案例--太空射击游戏总结
- Office无法卸载的最简单解决方法
- C语言中isalnum()函数和isalpha()函数的对比使用
- 改变工作目录
- 人脸识别
- Java中FileInputStream,FileReader等的区别
- Atitit.执行cli cmd的原理与调试
- phonegap原理分析-ios版