1泼差、歸檔有什么用构诚?
(如果不想知道有什么用蚌斩,直接跳到第三點)
將復(fù)雜對象寫入文件,需要時再讀取唤反。歸檔操作進(jìn)行的是深復(fù)制(如果不清楚什么是深復(fù)制凳寺,可以查閱相關(guān)資料,或者瞄一眼筆者另文的第一句)http://www.reibang.com/p/d2b7dd5f45dc
歸檔是一種序列化彤侍。
2肠缨、什么是序列化?
我也不懂盏阶,書上是這樣寫的:序列化對象是指可以被轉(zhuǎn)換為字節(jié)流以便于存儲到文件中或通過網(wǎng)絡(luò)進(jìn)行傳輸?shù)膶ο蟆?/em>(摘自《精通iOS開發(fā) (第7版)》)
3晒奕、一個簡單例子——數(shù)組的?歸檔
先隨便建一個數(shù)組,并輸出一下相關(guān)信息
NSArray *array1 = @[@"h", @"e", @"l", @"l", @"o"];
NSLog(@"array1: %@", array1); //輸出數(shù)組內(nèi)容
NSLog(@"array1: %p", array1); //輸出數(shù)組地址
歸檔——存數(shù)據(jù)
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:array1 forKey:@"array1"];
[archiver finishEncoding];
BOOL success = [data writeToFile:@"/存儲路徑.../archive" atomically:YES]);
所需材料:NSMutableData類和NSKeyedArchiver類名斟。
備注:歸檔不能用NSData替換NSMutableData類脑慧,因為初始化NSKeyedArchiver的方法接收的參數(shù)為NSMutableData。
反歸檔——讀數(shù)據(jù)
NSData *data2 = [[NSData alloc] initWithContentsOfFile:@"/存儲路徑.../archive"];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data2];
NSArray *array2 = [unarchiver decodeObjectForKey:@"array1"];
[unarchiver finishDecoding];
//輸出信息
NSLog(@"array2: %@", array2);
NSLog(@"array2: %p", array2);
所需材料:NSData類和NSKeyedUnarchiver類
備注:這里NSData可以換成NSMutableData砰盐,不過這里只是讀個數(shù)據(jù)而已闷袒,不會閑得蛋疼用可變的吧?
</br>
下面是快速方法岩梳,它們都是工廠方法囊骤,偷懶必備:
但如果需要多個類寫入同一個文件,就不能用以下的快速方法冀值。
</br>
歸檔快速方法1
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array1];
BOOL success = [data writeToFile:@"/存儲路徑.../archive" atomically:YES]);
所需材料:NSData類和NSKeyedArchiver類
備注:快捷方式不能用NSMutableData類替換NSData類也物,因為工廠方法返回值為NSData類
歸檔快速方法2
BOOL success = [NSKeyedArchiver archiveRootObject:array1 toFile:@"/存儲路徑.../archive"];
所需材料:NSKeyedArchiver類
反歸檔快速方法1
NSData *data2 = [[NSData alloc] initWithContentsOfFile:@"/存儲路徑.../archive"];
NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
所需材料:NSData類和NSKeyedUnarchiver類
備注:null
反歸檔快速方法2
NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/存儲路徑.../archive"];
所需材料:NSKeyedUnarchiver類
4、自定義類的歸檔
為了便于偷懶列疗,我使用歸檔和反歸檔的快捷方式
//Kardel.h
@interface Kardel : NSObject
@property (strong, nonatomic) NSString *name;
@end
//Kardel.m
#import "Kardel.h"
@implementation Kardel
-(instancetype)init {
self = [super init];
if (self) {
self.name = @"kardel";
}
return self;
}
@end
//main.m
#import <Foundation/Foundation.h>
#import "Kardel.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Kardel *kd = [[Kardel alloc] init];
//輸出信息
NSLog(@"name: %@", kd.name);
NSLog(@"kd: %p", kd);
//歸檔
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:kd];
if ([data writeToFile:@"/存儲路徑.../archive" atomically:YES]) {
NSLog(@"archive success");
}
//反歸檔
NSData *data2 = [[NSData alloc] initWithContentsOfFile:@"/存儲路徑.../archive"];
Kardel *anotherKd = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
//輸出信息
NSLog(@"name: %@", anotherKd.name);
NSLog(@"anotherKd: %p", anotherKd);
}
return 0;
}
[968:36263] name: kardel
[968:36263] kd: 0x100501520
[968:36263] -[Kardel encodeWithCoder:]: unrecognized selector sent to instance 0x100501520
[968:36263] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Kardel encodeWithCoder:]: unrecognized selector sent to instance 0x100501520'
......
雖然編譯通過滑蚯,但運行時會?崩潰。
日志上說自定義Kardel類沒有找到encodeWithCoder:這個方法,并且編譯器將錯誤指向下面這條語句:
為什么在執(zhí)行數(shù)組的歸檔時告材,沒有遇到這個問題呢坤次?
一定是因為NSArray里面有encodeWithCoder:這個方法。
NSArray是Foundation框架提供的创葡,它知道如何去歸檔數(shù)組≌闾撸現(xiàn)在你自定義了一個不知道什么鬼的類,要執(zhí)行歸檔灿渴,人家怎么知道要如何去操作你這個類的歸檔洛波?
所以實現(xiàn)encodeWithCoder:方法就是在告訴人家要用何種方式歸檔你的類;同理骚露,實現(xiàn)initWithCoder:方法解釋用何種方式反歸檔自定義類
Let's Do It
encodeWithCoder:和initWithCoder:方法都定義在NSCoding協(xié)議中蹬挤,因此必須先遵循這個協(xié)議:
@interface Kardel : NSObject <NSCoding>
然后實現(xiàn):
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"kNameKey"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"kNameKey"];
}
return self;
}
再次運行程序:
[1109:52987] name: kardel
[1109:52987] kd: 0x1031000f0
[1109:52987] archive success
[1109:52987] name: kardel
[1109:52987] anotherKd: 0x100300d10
上面自定義Kardel類是NSObject的子類,如果我們把Kardel改成是NSArray的子類棘幸。
@interface Kardel : NSArray <NSCoding>
那么我們就要修改這2個方法的第一條語句了:
- (void)encodeWithCoder:(NSCoder *)aCoder {
[super encodeWithCoder:aCoder];
[aCoder encodeObject:self.name forKey:@"kNameKey"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"kNameKey"];
}
return self;
}
所以焰扳,當(dāng)自定義類(其實不限于自定義類)的父類實現(xiàn)了NSCoding的方法,必須先調(diào)用父類的encodeWithCoder:方法和initWithCoder:方法
另误续,在為自定義類歸檔時遵循了NSCoding協(xié)議吨悍,可以一同遵循NSCopying協(xié)議,實現(xiàn)copyWithZone:
方法(遵循NSCopying對于任何數(shù)據(jù)類型對象來說都是非常好的事情——該建議出自《精通iOS開發(fā)(第7版)》)