归档与解归档

时间:2022-02-15 16:21:19

//归档方法:[NSKeyArchiver archiveRootObject:__toFile:@"填写路径"];
//解归档: [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/q/Desktop/array.data"];

//数组归档*****************************
//构建二维数组
NSMutableArray *array1=[[NSMutableArray alloc]init];
for(int i=0;i<3;i++){
NSNumber *number=[NSNumber numberWithInt:100+i];
[array1 addObject:number];
}
NSMutableArray *array2=[[NSMutableArray alloc]init];
for(int i=0;i<4;i++){
[array2 addObject:[NSString stringWithFormat:@"string%i",i+1]];
}
NSArray * array=@[array1,array2];
//将二维数组归档
BOOL ret=[NSKeyedArchiver archiveRootObject:array toFile:@"/Users/q/Desktop/array.data"];
NSLog(@"ret:%d",ret);
//解归档
NSArray *array3=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/q/Desktop/array.data"];
NSLog(@"array3:%@",array3);
//字典归档**************************
//构建一个词典
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
[dict setObject:@"zhangsan" forKey:@"username"];
[dict setObject:@"123456" forKey:@"password"];
[dict setObject:[NSDate date] forKey:@"date"];
NSArray * array=@[@"zhangsan@163.com",@"zhangsan@qq.com"];
[dict setObject:array forKey:@"email"];
//将词典对象归档
BOOL ret=[NSKeyedArchiver archiveRootObject:dict toFile:@"/Users/q/Desktop/dict.data"];
NSLog(@"ret=%d",ret);
//解归档数据到词典
NSDictionary *dict2=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/q/Desktop/dict.data"];
NSLog(@"dict:%@",dict2);

//类归档*************************************

//自定义的对象需要归档,类必须遵守归档协议,归档时会调用协议方法encodeWithCoder,如这个Person类
//.h文件中写;
@interface Person : NSObject<NSCoding>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)int ID;
//.m文件中
-(NSString *)description
{
return [NSString stringWithFormat:@"name is %@, ID is %d",_name,_ID];
}
//归档对象时调用此方法 * 当一个对象要归档进沙盒中时,就会调用这个方法
//* 目的:在这个方法中说明这个对象的哪些属性要存进沙盒
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInt:_ID forKey:@"ID"];

}
/**
* 当从沙盒中解档一个对象时(从沙盒中加载一个对象时),就会调用这个方法
* 目的:在这个方法中说明沙盒中的属性该怎么解析(需要取出哪些属性)
*/

-(id)initWithCoder:(NSCoder *)aDecoder
{
self=[super init];//如果父类也遵守归档协议,此处[super initWithCode:aDecode];
if(self){
self.name=[aDecoder decodeObjectForKey:@"name"];
self.ID=[aDecoder decodeIntForKey:@"ID"];
}
return self;
}

//调用归档和解归档的方法
Person *p=[[Person alloc]init];
p.name=@"zhangsan";
p.ID=1001;
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"asd.archive"];
//将p对象归档存入文件,p类必须遵守NSCoding协议,归档时会调用协议方法encodeWithCoder
BOOL ret=[NSKeyedArchiver archiveRootObject:p toFile:path];
NSLog(@"ret:%d",ret);
//解归档,读取文件内容到Person对象
Person *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"p2:%@",p2);

//如果plist文件可以直接写入沙盒
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"asd.plist"];

[responseObject writeToFile:path atomically:YES];
//这里的responseObject为数组,或者字典,并非对象

//取出方法字典和数组两种
NSArray *array= [NSArray arrayWithContentsOfFile:path];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];