归档
实现序列化机制 用某种格式把对象保存在指定文件 方便以后文件恢复
1 把对象写入指定文件 归档
2 把这种形式的文件从文件中恢复出来 解归档
//把字典dict写入File
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"1" forKey:@"one"];
[dict setObject:@"2" forKey:@"two"];
[dict setObject:@"3" forKey:@"three"];
BOOL ret = [NSKeyedArchiver archiveRootObject:dict toFile:@"指定文件名"];
if (ret)
{
NSLog(@"归档成功");
}
else
{
NSLog(@"归档失败");
}
//解归档
id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:@"指定文件名"];
if ([obj isKindOfClass:[NSDictionary class]]) //判断数据类型
{
NSLog(@"dict = %@",obj);
}