/*1.将通讯录类(姓名、电话),添加归档和解归档方法,即,添加储存信息到磁盘的方法。*/
#import <Foundation/Foundation.h>
#import "ConTact.h"
#define PATH @"/Users/qianfeng/Desktop/hehe/2.plist"
int main(int argc, const char * argv[]) {
@autoreleasepool {
ConTact *contact = [[ConTact alloc] init];
contact.name = @"xiaowang";
contact.tel = @"135";
//创建可变二进制文件
NSMutableData *mdata = [[NSMutableData alloc] init];
//创建归档对象
NSKeyedArchiver *keyarchiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mdata];
//归档对象
[keyarchiver encodeObject:contact forKey:@"name"];
//完成归档
[keyarchiver finishEncoding];
//写入文件
[mdata writeToFile:PATH atomically:YES];
printf("............................................\n");
//读取二进制文件
NSData *data = [[NSData alloc] initWithContentsOfFile:PATH];
//创建解归档对象
NSKeyedUnarchiver *keyunarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//解归档
ConTact *c = [keyunarchiver decodeObjectForKey:@"name"];
//解除归档
[keyunarchiver finishDecoding];
NSLog(@"%@,%@",c.name,c.tel);
}
return 0;
}