//KeyArchiverTest序列化
//存儲(chǔ)plist文件先找到要存儲(chǔ)的path
NSString * filePath = [self filePath];
People * p = [[People alloc] init];
//存儲(chǔ)數(shù)據(jù)的優(yōu)先級(jí)按字節(jié)大小
p.name=@"序列化";
p.longName=@"序列化與反序列化";
p.age= 18;
//先對(duì)它進(jìn)行序列化NSKeyedArchiver歸檔(轉(zhuǎn)化為NSData) NSKeyedArchiver調(diào)用方法archivedDataWithRootObject
//1.BOOL類(lèi)型的方法轉(zhuǎn)化boolValue
//NSData * peopleData1 = [[NSKeyedArchiver archiveRootObject:p toFile:filePath]boolValue];
//2.NSKeyedArchiver
NSData* peopleData = [NSKeyedArchiver archivedDataWithRootObject:p];
//把二進(jìn)制數(shù)寫(xiě)入本地
[peopleData writeToFile:filePath atomically:YES];
//NSLog(@"%@",peopleData);
//反序列化NSKeyedUnarchiver
//NSData * data = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//首先要把二進(jìn)制數(shù)據(jù)從文件中讀取出來(lái)
NSData* data = [NSData dataWithContentsOfFile:filePath];
//對(duì)讀取出來(lái)的二進(jìn)制數(shù)進(jìn)行反序列化
People *p1 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"%@",p1.longName);
NSLog(@"%@",p1.name);
NSString* str = [[NSString alloc] initWithData:people Dataencoding:NSUTF8StringEncoding];
NSLog(@"%@",str);//null
}
- (NSString *)filePath {
//獲得沙盒根目錄
NSString * hamePath =NSHomeDirectory();
//拼接文件路徑
NSString * filePath = [hamePathstringByAppendingString:@"/Documents/arra.plist"];
//返回類(lèi)型
NSLog(@"%@",filePath);
return filePath;
}