(一)归档
//0.
准备数据
NSArray
* dataArr =
@[
@"Jack"
,
@20
,
@[
@"Swift"
,
@"OC"
]]
;
//1. 准备一个可变的 NSMutableData 类型
NSMutableData *mutableData = [[ NSMutableData alloc ] init ];
//2. 创建 NSKeyedArchive 对象
NSKeyedArchiver * archive = [[ NSKeyedArchiver alloc ] initForWritingWithMutableData :mutableData];
//3. 对数据进行编码
[archive encodeObject :dataArr forKey : @"arrayKey" ];
//4. 执行 完成编码的任务
[archive finishEncoding ];
//5. 写入文件
NSString * docPath = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) firstObject ];
//1. 准备一个可变的 NSMutableData 类型
NSMutableData *mutableData = [[ NSMutableData alloc ] init ];
//2. 创建 NSKeyedArchive 对象
NSKeyedArchiver * archive = [[ NSKeyedArchiver alloc ] initForWritingWithMutableData :mutableData];
//3. 对数据进行编码
[archive encodeObject :dataArr forKey : @"arrayKey" ];
//4. 执行 完成编码的任务
[archive finishEncoding ];
//5. 写入文件
NSString * docPath = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) firstObject ];
NSString
* filePath = [docPath
stringByAppendingPathComponent
:
@"archive"
];
[mutableData
writeToFile
:filePath
atomically
:
YES
];
(二)解档
//1.
从指定的文件读取数据(
读取
NSData数据
到程序中)
NSMutableData
* readData = [
NSMutableData
dataWithContentsOfFile
:filePath];
//2. 创建 NSKeyedUnarchive 对象
NSKeyedUnarchiver * unarchiver = [[ NSKeyedUnarchiver alloc ] initForReadingWithData :readData];
//3. 对数据进行解码
NSArray * readArr = [unarchiver decodeObjectForKey : @"arrayKey" ];
//4. 执行完成解码
//2. 创建 NSKeyedUnarchive 对象
NSKeyedUnarchiver * unarchiver = [[ NSKeyedUnarchiver alloc ] initForReadingWithData :readData];
//3. 对数据进行解码
NSArray * readArr = [unarchiver decodeObjectForKey : @"arrayKey" ];
//4. 执行完成解码
[unarchiver
finishDecoding
];
(三)自定义数据类型的编码和解码
- 前提:必须遵守NSCoding协议
- 步骤:
- ①遵守NSCoding协议
- ②实现协议的方法(为属性编码和解码)
//
把所有的属性进行编码操作
- ( void )encodeWithCoder:( NSCoder *)aCoder{
[aCoder encodeObject : self . name forKey : @"nameKey" ];
[aCoder encodeInteger : self . age forKey : @"ageKey" ];
}
// 把所有的属性进行解码操作
- ( nullable instancetype )initWithCoder:( NSCoder *)aDecoder{
if ( self = [ super init ]) {
self . name = [aDecoder decodeObjectForKey : @"nameKey" ];
self . age = [aDecoder decodeIntegerForKey : @"ageKey" ];
}
return self ;
- ( void )encodeWithCoder:( NSCoder *)aCoder{
[aCoder encodeObject : self . name forKey : @"nameKey" ];
[aCoder encodeInteger : self . age forKey : @"ageKey" ];
}
// 把所有的属性进行解码操作
- ( nullable instancetype )initWithCoder:( NSCoder *)aDecoder{
if ( self = [ super init ]) {
self . name = [aDecoder decodeObjectForKey : @"nameKey" ];
self . age = [aDecoder decodeIntegerForKey : @"ageKey" ];
}
return self ;
}
- ③基本的(归档/解档)的9步
(四)结合NSUserDefaults的自定义数据类型的 归档/解档
- 原理:将自定义类型转为NSData类型的数据进行存储
- 实现步骤:
- ①遵守NSCoding协议
- ②实现协议的方法(为属性编码和解码)
- ③使用NSKeyedArchiver 和 NSKeyedUnarchiver将自定义类型的对象进行转化
//
创建自定义的模型对象
Person * person = [[ Person alloc ] initWithName : @"Jack" andAge : 10 ];
//1. 写入
//1.1 将自定义对象转换为二进制数据
// 调用 NSKeyedArchiver 的类方法
NSData * wData = [ NSKeyedArchiver archivedDataWithRootObject :person];
//1.2 设置键值对
[[ NSUserDefaults standardUserDefaults ] setObject :wData forKey : @"dataPerson" ];
//1.3 调用同步方法(强制写入)
[[ NSUserDefaults standardUserDefaults ] synchronize ];
//2. 读取
//2.1 从文件中读取数据
NSData * readData = [[ NSUserDefaults standardUserDefaults ] objectForKey : @"dataPerson" ];
//2.2 返回一个解码完毕的 Person 类型对象
Person * p = [ NSKeyedUnarchiver unarchiveObjectWithData :readData];
Person * person = [[ Person alloc ] initWithName : @"Jack" andAge : 10 ];
//1. 写入
//1.1 将自定义对象转换为二进制数据
// 调用 NSKeyedArchiver 的类方法
NSData * wData = [ NSKeyedArchiver archivedDataWithRootObject :person];
//1.2 设置键值对
[[ NSUserDefaults standardUserDefaults ] setObject :wData forKey : @"dataPerson" ];
//1.3 调用同步方法(强制写入)
[[ NSUserDefaults standardUserDefaults ] synchronize ];
//2. 读取
//2.1 从文件中读取数据
NSData * readData = [[ NSUserDefaults standardUserDefaults ] objectForKey : @"dataPerson" ];
//2.2 返回一个解码完毕的 Person 类型对象
Person * p = [ NSKeyedUnarchiver unarchiveObjectWithData :readData];
//3.检验
NSLog
(
@"%ld"
,p.
age
);