标签:
/*2.创建一个数组,数组中保存一批量的学生信息,把该数组信息储存到磁盘上,并能解归档出来*/
#import <Foundation/Foundation.h>
#define path @"/Users/qianfeng/Desktop/hehe/5.plist"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *marray = [[NSMutableArray alloc] init];
[marray addObject:@"1"];
[marray addObject:@"2"];
[marray addObject:@"王"];
[marray addObject:@"4"];
[marray addObject:@"5"];
// [NSKeyedArchiver archiveRootObject:marray toFile:path];
// NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// NSLog(@"%@",array);
//创建一个可变二进制数据
NSMutableData *data = [[NSMutableData alloc] init];
//创建一个归档对象
NSKeyedArchiver *keyarchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//归档
[keyarchiver encodeObject:marray forKey:@"wang"];
//完成归档
[keyarchiver finishEncoding];
[data writeToFile:path atomically:YES];
printf("..........\n");
//反归档
//二进制文件读取数据
NSData *data1 = [[NSData alloc] initWithContentsOfFile:path];
//创建归档对象
NSKeyedUnarchiver *keyunarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data1];
//解归档
NSArray *array = [keyunarchiver decodeObjectForKey:@"wang"];
//结束归档
[keyunarchiver finishDecoding];
for(id obj in array)
{
NSLog(@"%@",obj);
}
NSLog(@"%@",array);
}
return 0;
}
创建一个数组,数组中保存一批量的学生信息,把该数组信息储存到磁盘上,并能解归档出来
标签:
原文地址:http://www.cnblogs.com/wanghengheng/p/4798890.html