归档反归档 - 本地沙盒存储复杂对象
NSString,NSNumber,NSArray,NSDictionary等简单对象可以方便的存储在本地沙盒文件中,而复杂的对象,诸如属性、方法较多的类对象,就需要使用归档反归档方法(序列化与反序列化),以NSData方式进行存储。
归档反归档,主要的操作顺序是:编码(enCoding) -> 归档(archiver) -> 解码(deCoding) -> 反归档(unarchiver)
1.编码与解码的方法,需要在类的声明文件中接受<NSCoding>协议,在实现文件.m中完成协议方法:
1 /* 2 * Model.h 接受NSCoding协议 3 */ 4 5 #import <Foundation/Foundation.h> 6 #import <UIKit/UIKit.h> 7 8 @interface Model : NSObject<NSCoding> 9 @property(nonatomic,strong)NSString *name; 10 @property(nonatomic,assign)NSInteger num; 11 12 - (instancetype)initWithName:(NSString *)name num:(NSInteger)num; 13 @end
1 /* 2 * Model.m 实现编码与解码的协议方法 3 */ 4 5 #import "Model.h" 6 7 @implementation Model 8 - (instancetype)initWithName:(NSString *)name num:(NSInteger)num{ 9 self = [super init]; 10 if(self){ 11 self.name = name; 12 self.num = num; 13 } 14 return self; 15 } 16 17 // 编码 18 - (void)encodeWithCoder:(NSCoder *)aCoder{ 19 [aCoder encodeObject:self.name forKey:@"name"]; 20 [aCoder encodeObject:@(self.num) forKey:@"num"]; 21 } 22 23 // 解码 24 - (instancetype)initWithCoder:(NSCoder *)aDecoder{ 25 self = [super init]; 26 if(self){ 27 self.name = [aDecoder decodeObjectForKey:@"name"]; 28 self.num = [[aDecoder decodeObjectForKey:@"num"] integerValue]; 29 } 30 return self; 31 } 32 33 @end
2.归档 - 保存复杂对象;反归档 - 获取对象
1 #import "ViewController.h" 2 #import "Model.h" 3 4 @interface ViewController () 5 @property (weak, nonatomic) IBOutlet UIImageView *image; 6 @property (weak, nonatomic) IBOutlet UITextField *nameText; 7 @property (nonatomic,strong)NSString *path; 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 // 获取沙盒路径 16 self.path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"data.data"]; 17 } 18 19 // 归档保存对象 20 - (IBAction)save:(UIButton *)sender { 21 // 创建对象 22 Model *m = [[Model alloc] initWithName:@"测试" num:101]; 23 24 // 创建NSData(可变),NSKeyedArchiver(实现归档的对象) 25 NSMutableData *data = [NSMutableData data]; 26 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; 27 28 // 对象进行编码归档 29 [archiver encodeObject:m forKey:@"m"]; 30 31 // 归档完成(必需) 32 [archiver finishEncoding]; 33 34 // 写入沙盒路径文件中 35 [data writeToFile:self.path atomically:YES]; 36 } 37 38 // 反归档解码获取对象 39 - (IBAction)get:(UIButton *)sender { 40 // 创建NSData(可变), NSKeyedUnarchiver(实现反归档的对象) 41 NSMutableData *data = [NSMutableData dataWithContentsOfFile:self.path]; 42 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 43 44 // 反归档解码获取对象 45 Model *m = [unarchiver decodeObjectForKey:@"m"]; 46 47 self.nameText.text = [NSString stringWithFormat:@"%@ %ld", m.name, m.num]; 48 } 49 50 - (void)didReceiveMemoryWarning { 51 [super didReceiveMemoryWarning]; 52 // Dispose of any resources that can be recreated. 53 } 54 55 @end
*.主要沙盒路径:
NSHomeDirectory----------------------->沙盒主路径
NSDocumentDirectory------------------->Documents文件夹
NSLibraryDirectory-------------------->Library文件夹
NSCachesDirectory--------------------->Caches文件夹
NSTemporaryDirectory------------------>tmp文件夹