简单的解档 归档

//简单的解档归档

//定义Model类

//签协议

@property(nonatomic,strong)NSString *name;//定义属性

//协议方法

- (void)encodeWithCoder:(NSCoder *)aCoder{

[aCoder encodeObject:self.name forKey:@"name"];

}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{

if (self=[super init]) {

[aDecoder decodeObjectForKey:@"name"];

}

return self;

}

//ViewController

//初始化model

Model *m=[[Model alloc]init];

m.name=@"嘿嘿你好";

//归档

NSString *path=[NSHomeDirectory()stringByAppendingPathComponent:@"atany.archiver"];

NSLog( @"%@",path);

BOOL bol=[NSKeyedArchiver archiveRootObject:m toFile:path];

if (bol) {

NSLog(@"归档成功");

}else{

NSLog(@"归档失败");

}

//解档

Model *mm=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

NSLog(@"%@",mm.name);

你可能感兴趣的:(简单的解档 归档)