iOS-NSCoding归档
2016-02-23 17:36
543 查看
归档比NSUserdefaults多了存储对象
例子:新建了一个学生类 .h文件
#import <Foundation/Foundation.h>
@interface ybStudent : NSObject <NSCoding>//遵守协议
/**名字*/
@property (nonatomic,copy)NSString *name;
/**年龄*/
@property (nonatomic,assign)int age;
/**身高*/
@property (nonatomic,assign)double height;
@end
学生类 .m文件
#import "ybStudent.h"
@implementation ybStudent
/**
* 将某个对象写入文件时会调用
* 在这个方法中说说清楚哪些属性需要存储
*/
- (void)encodeWithCoder:(NSCoder *)aCoder
{
//存储的类型需要和属性类型匹配
[aCoder encodeObject:self.name forKey:@"myName"];
[aCoder encodeDouble:self.height forKey:@"myHeight"];
[aCoder encodeInt:self.age forKey:@"myAge"];
}
/**
* 从文件中解析对象时会调用
* 在这个方法中说清楚哪些属性需要读取
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"myName"];
self.age = [aDecoder decodeIntForKey:@"myAge"];
self.height = [aDecoder decodeDoubleForKey:@"myHeight"];
}
return self;
}
@end
控制器里面两个按钮,分别为写入数据和读取数据
- (IBAction)save:(UIButton *)sender
{
ybStudent *student = [[ybStudent alloc] init];
student.name = @"Tom";
student.age = 18;
student.height = 1.75;
//获得Documents的全路径
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"student.data"];
//将对象归档
[NSKeyedArchiver archiveRootObject:student toFile:path];
}
- (IBAction)read:(UIButton *)sender
{
//从文件中读取
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"student.data"];
ybStudent *student = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"name---%@,age---%d,height---%f",student.name,student.age,student.height);
}
例子:新建了一个学生类 .h文件
#import <Foundation/Foundation.h>
@interface ybStudent : NSObject <NSCoding>//遵守协议
/**名字*/
@property (nonatomic,copy)NSString *name;
/**年龄*/
@property (nonatomic,assign)int age;
/**身高*/
@property (nonatomic,assign)double height;
@end
学生类 .m文件
#import "ybStudent.h"
@implementation ybStudent
/**
* 将某个对象写入文件时会调用
* 在这个方法中说说清楚哪些属性需要存储
*/
- (void)encodeWithCoder:(NSCoder *)aCoder
{
//存储的类型需要和属性类型匹配
[aCoder encodeObject:self.name forKey:@"myName"];
[aCoder encodeDouble:self.height forKey:@"myHeight"];
[aCoder encodeInt:self.age forKey:@"myAge"];
}
/**
* 从文件中解析对象时会调用
* 在这个方法中说清楚哪些属性需要读取
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"myName"];
self.age = [aDecoder decodeIntForKey:@"myAge"];
self.height = [aDecoder decodeDoubleForKey:@"myHeight"];
}
return self;
}
@end
控制器里面两个按钮,分别为写入数据和读取数据
- (IBAction)save:(UIButton *)sender
{
ybStudent *student = [[ybStudent alloc] init];
student.name = @"Tom";
student.age = 18;
student.height = 1.75;
//获得Documents的全路径
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"student.data"];
//将对象归档
[NSKeyedArchiver archiveRootObject:student toFile:path];
}
- (IBAction)read:(UIButton *)sender
{
//从文件中读取
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"student.data"];
ybStudent *student = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"name---%@,age---%d,height---%f",student.name,student.age,student.height);
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Tomcat端口被占用解决方法(不用重启)
- “传奇”图象数据存储方式
- 超大数据量存储常用数据库分表分库算法总结
- SQL Server误区30日谈 第18天 有关FileStream的存储,垃圾回收以及其它
- C++实现图的邻接表存储和广度优先遍历实例分析
- 详解Android文件存储
- C#调用sql2000存储过程方法小结
- PHP 存储文本换行实现方法
- 注册表中存储数据库链接字符串的方法
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略