NSCoding、NSSecureCoding、NSCopying

NSCoding

存与取

NSCoding是把数据存储在iOS和Mac OS上的一种极其简单和方便的方式,它把模型对象直接转变成一个文件,然后再把这个文件重新加载到内存里,并不需要任何文件解析和序列化的逻辑。如果要把对象保存到一个数据文件中(假设这个对象实现了NSCoding协议),你可以这样做咯:

//存到本地
Book *book = [[Book alloc] init];
[NSKeyedArchiver archiveRootObject:book toFile:filePath];

//从本地取出
Book *theBook = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

如何写遵循NSCoding协议的类

实现两个方法- (void)encodeWithCoder:(NSCoder *)aCoder- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder,举个刚刚Book类的栗子:

- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.title forKey:@"title"];
    [aCoder encodeObject:self.author forKey:@"author"];
    [aCoder encodeBool:self.isPublished forKey:@"isPublished"];    
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.author = [aDecoder decodeObjectForKey:@"author"];
        self.isPublished = [aDecoder decodeBoolForKey:@"isPublished"];
    }
    return self;
}

NSSecureCoding

NSSecureCoding是NSCoding的变种,因为NSCoding毕竟不太安全,大部分支持NSCoding的系统对象都已经升级到支持NSSecureCoding了,如AFNetworking的AFURLSessionManager,下面老衲举个栗子:

存与取

NSData *data = [NSData dataWithContentsOfFile:filePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[unarchiver setRequiresSecureCoding:YES];
 
//解码
Foo *someFoo = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey];

如何遵循协议

在原来encodeWithCoderinitWithCoder的基础上增加supportsSecureCoding,如下

- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.title forKey:@"title"];
    [aCoder encodeObject:self.author forKey:@"author"];
    [aCoder encodeBool:self.isPublished forKey:@"isPublished"];
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.author = [aDecoder decodeObjectForKey:@"author"];
        self.isPublished = [aDecoder decodeBoolForKey:@"isPublished"];
    }
    return self;
}

+ (BOOL)supportsSecureCoding{
    return YES;
}

NSCopying

NSCopying是一个与对象拷贝有关的协议。如果想让一个类的对象支持拷贝,就需要让该类实现NSCopying协议。NSCopying协议中的声明的方法只有一个- (id)copyWithZone:(NSZone *)zone。当我们的类实现了NSCopying协议,通过类的对象调用copy方法时,copy方法就会去调用我们实现的- (id)copyWithZone:(NSZone *)zone方法,实现拷贝功能。比如AFN里对session的configuration进行了拷贝:

- (instancetype)copyWithZone:(NSZone *)zone {
    return [[[self class] allocWithZone:zone] initWithSessionConfiguration:self.session.configuration];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文为转载: 作者:zyydeveloper 链接:http://www.jianshu.com/p/5f776a...
    Buddha_like阅读 4,479评论 0 2
  • 下面是我最近两年学习OC中的一些基础知识,对于学习OC基础知识的人可能有些帮助,拿出来分享一下,还是那句话不喜勿喷...
    小小赵纸农阅读 7,634评论 1 7
  • 对象的复制 浅复制和深复制 用与一个实例对象相同的内容,生成一个新对象,这个过程一般称为复制。 只复制对象的指针称...
    陈_振阅读 3,287评论 0 0
  • 对模型对象进行归档(objective-c) 对模型对象进行归档是4种将数据持久存储在iOS文件系统的机制之一 使...
    Laugamjeon阅读 6,128评论 0 1
  • 一、数据持久化概述 数据持久化就是数据的永久存储。其本质是将数据保存为文件,存到程序的沙盒中。 1、数据持久化的方...
    lilinjianshu阅读 3,919评论 0 1

友情链接更多精彩内容