#若要确保继承的实例变量也被编码:
[super encodeWithCoder:encoder];
#若要确保继承的实例变量也被解码:
[super initWithCoder:encoder];```
//获取根目录
NSString *homeDictionary = NSHomeDirectory();
NSString *homePath = [homeDictionary stringByAppendingPathComponent:@"atany.archiver"];
//添加储存的文件名
//方式一:通过data数据归档,在将数据写入文件
NSData *data= [NSKeyedArchiver archivedDataWithRootObject:@"123"];
[data writeToFile:homePath atomically:YES];
//方式二:直接写入文件
[NSKeyedArchiver archiveRootObject:@"456" toFile:homePath];
//方式一和方式二的效果完全一样 只是解归档的时候不同
//方式一的解归档:先获取data数据,在进行data数据的解归档
NSLog(@"%@",[NSKeyedUnarchiver unarchiveObjectWithData:data]);
//方式二的解归档:直接解文件中的归档
NSLog(@"%@",[NSKeyedUnarchiver unarchiveObjectWithFile:homePath]);
//获取根目录
NSString *homeDictionary = NSHomeDirectory();
NSString *homePath = [homeDictionary stringByAppendingPathComponent:@"atany.archiver"];
//添加储存的文件名
//这里创建一个可变的data对象作为归档的容器
NSMutableData * data = [[NSMutableData alloc]init];
//创建一个归档对象,归档后写入data数据
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//对下面的字符串和int值进行归档序列化
[archiver encodeObject:@"jaki" forKey:@"name"];
[archiver encodeInt:24 forKey:@"age"];
//写入data
[archiver finishEncoding];
//写入文件
[data writeToFile:homePath atomically:YES];
//创建解归档的反序列化对象
NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
//进行反序列化
NSString * name = [unarchiver decodeObjectForKey:@"name"];
int age = [unarchiver decodeIntForKey:@"age"];
//打印信息
NSLog(@"\nname:%@\nage:%d",name,age);
--http://my.oschina.net/u/2340880/blog/514330