NSCoding、NSSecureCoding、NSCopying

NSCoding

存與取

NSCoding是把數(shù)據(jù)存儲在iOS和Mac OS上的一種極其簡單和方便的方式,它把模型對象直接轉(zhuǎn)變成一個文件,然后再把這個文件重新加載到內(nèi)存里,并不需要任何文件解析和序列化的邏輯。如果要把對象保存到一個數(shù)據(jù)文件中(假設(shè)這個對象實現(xiàn)了NSCoding協(xié)議),你可以這樣做咯:

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

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

如何寫遵循NSCoding協(xié)議的類

實現(xiàn)兩個方法- (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的系統(tǒng)對象都已經(jīng)升級到支持NSSecureCoding了,如AFNetworking的AFURLSessionManager,下面老衲舉個栗子:

存與取

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

如何遵循協(xié)議

在原來encodeWithCoderinitWithCoder的基礎(chǔ)上增加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是一個與對象拷貝有關(guān)的協(xié)議。如果想讓一個類的對象支持拷貝,就需要讓該類實現(xiàn)NSCopying協(xié)議。NSCopying協(xié)議中的聲明的方法只有一個- (id)copyWithZone:(NSZone *)zone。當(dāng)我們的類實現(xiàn)了NSCopying協(xié)議,通過類的對象調(diào)用copy方法時,copy方法就會去調(diào)用我們實現(xiàn)的- (id)copyWithZone:(NSZone *)zone方法,實現(xiàn)拷貝功能。比如AFN里對session的configuration進(jìn)行了拷貝:

- (instancetype)copyWithZone:(NSZone *)zone {
    return [[[self class] allocWithZone:zone] initWithSessionConfiguration:self.session.configuration];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文為轉(zhuǎn)載: 作者:zyydeveloper 鏈接:http://www.itdecent.cn/p/5f776a...
    Buddha_like閱讀 1,017評論 0 2
  • 下面是我最近兩年學(xué)習(xí)OC中的一些基礎(chǔ)知識,對于學(xué)習(xí)OC基礎(chǔ)知識的人可能有些幫助,拿出來分享一下,還是那句話不喜勿噴...
    小小趙紙農(nóng)閱讀 2,821評論 1 7
  • 對象的復(fù)制 淺復(fù)制和深復(fù)制 用與一個實例對象相同的內(nèi)容,生成一個新對象,這個過程一般稱為復(fù)制。 只復(fù)制對象的指針稱...
    陳_振閱讀 522評論 0 0
  • 對模型對象進(jìn)行歸檔(objective-c) 對模型對象進(jìn)行歸檔是4種將數(shù)據(jù)持久存儲在iOS文件系統(tǒng)的機(jī)制之一 使...
    Laugamjeon閱讀 2,093評論 0 1
  • 一、數(shù)據(jù)持久化概述 數(shù)據(jù)持久化就是數(shù)據(jù)的永久存儲。其本質(zhì)是將數(shù)據(jù)保存為文件,存到程序的沙盒中。 1、數(shù)據(jù)持久化的方...
    lilinjianshu閱讀 739評論 0 1

友情鏈接更多精彩內(nèi)容