iOS归档 反归档 Archiving Unarchiver

Archiving: 归档
支持的类型:只要遵守NSCoding协议的对象都可以使用归档
归档步骤(写)
a. 准备可变数据对象NSMutableData
NSMutableData *mutableData = [NSMutableData data];
b. 创建归档对象NSKeyedArchiver
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
c. 针对要保存的对象进行编码encode
[archiver encodeObject:dataArray forKey:@”user”];
执行归档动作!!!![archiver finishEncoding];
d. 将数据写到文件中
[mutableData writeToFile:self.archivingFilePath atomically:YES];
反归档步骤(读)
a. 准备可变数据对象NSMutableData
NSData *data = [NSData dataWithContentsOfFile:self.archivingFilePath];
b. 创建反归档对象NSKeyedUnarchiver
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
c. 针对要读取的对象进行解码
NSArray *array = [unarchiver decodeObjectForKey:@”user”];
d. 执行反归档的动作
[unarchiver finishDecoding];

你可能感兴趣的:(iOS归档 反归档 Archiving Unarchiver)