文件归档NSKeyedArchiver,可以实现对文件的归档,序列化,反归档NSKeyedUnarchiver
//
// main.m
// IOS150623_objectiveC_归档
//
// Created by PengJunlong on 15/6/23.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import <Foundation/Foundation.h>
//*************************
//* 归档 *
//*************************
//归档:把对象数据存储到文件的过程,不能归档自定义的类型(转化成字节码存储)
//反归档:从文件中读取数据到对象的过程
int main(int argc, const char * argv[]) {
@autoreleasepool {
//#if 0
//-----------------用系统方法归档------------------
//方法一:
NSMutableDictionary *mulDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
[mulDict setValue:[NSDate date] forKey:@"date"];
[mulDict setValue:[NSNumber numberWithInt:232] forKey:@"num"];
[mulDict setValue:[@"IOSApple" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"];
BOOL ret = [NSKeyedArchiver archiveRootObject:mulDict toFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/arch.txt"];
if (ret) {
NSLog(@"归档成功");
}
else
{
NSLog(@"归档失败");
}
//方法二:
NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:mulDict];
ret = [data1 writeToFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/archdata.txt" atomically:YES];
if (ret) {
NSLog(@"归档data成功");
}
else
{
NSLog(@"归档data失败");
}
//#else
//反归档
//方法一:
NSMutableDictionary *mulDict1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/arch.txt"];
if (mulDict1) {
NSLog(@"mulDict1 = %@",mulDict1);
}
//方法二:
NSData *da = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/archdata.txt"];
NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:da];
if (dic) {
NSLog(@"dic = %@",dic);
}
//#endif
//-------------手动归档------------------
//归档
//创建一个空的可变data对象,一定是可变的
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSArray *array = [NSArray arrayWithObjects:@"one",@"two",[NSNumber numberWithInt:23],[NSDate date],[@"I love china" dataUsingEncoding:NSUTF8StringEncoding], nil];
NSMutableDictionary *dictionary1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"5",@"five",@"6",@"six",[NSNumber numberWithFloat:23.33],@"numfloat",[NSNumber numberWithInt:233],@"num", nil];
//对数组及子字典进行编码
[archiver encodeObject:array forKey:@"array"];
[archiver encodeObject:dictionary1 forKey:@"dictionary"];
//完成编码,一点要finishEncoding
[archiver finishEncoding];
//写入文件
int ret1 = [data writeToFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/manualarch.txt" atomically:YES];
if (ret1) {
NSLog(@"文件归档成功");
}
else
{
NSLog(@"归档失败");
}
//反归档
NSData *data2 = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/manualarch.txt"];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data2];
NSArray *array1 = [unarchiver decodeObjectForKey:@"array"];
NSMutableDictionary *dictionary2 = [unarchiver decodeObjectForKey:@"dictionary"];
//完成解码
[unarchiver finishDecoding];
NSLog(@"Array= %@",array1);
NSLog(@"==================");
NSLog(@"Dictionary = %@",dictionary2);
}
return 0;
}