标签:
1 //归档:将对象数据存储到文件的过程 NSArchiver 2 //反归档:从文件中读取数据到对象中的过程 3 4 int main(int argc, const char * argv[]) { 5 @autoreleasepool { 6 7 #if 0 8 //使用系统的方法进行归档 NSArchiver 9 NSDictionary *mutDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"111",@"one",@"222",@"two", nil]; 10 [mutDic setValue:[NSDate date] forKey:@"date"]; 11 [mutDic setValue:[NSNumber numberWithInteger:8889] forKey:@"num"]; 12 [mutDic setValue:[@"ios" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"]; 13 14 BOOL flag = [NSArchiver archiveRootObject:mutDic toFile:@"/Users/qianfeng/Desktop/day21-OC11_时间类/day21_OC11_归档与反归档/arch.txt"]; 15 if (flag) { 16 NSLog(@"文件归档成功"); 17 }else{ 18 NSLog(@"文件归档失败"); 19 } 20 21 #endif 22 23 #if 0 24 //使用系统进行反归档 NSUnarchiver 25 NSDictionary *dict1 = [NSUnarchiver unarchiveObjectWithFile:@"/Users/qianfeng/Desktop/day21-OC11_时间类/day21_OC11_归档与反归档/arch.txt"]; 26 if (dict1) { 27 NSLog(@"dict = %@",dict1); 28 } 29 #endif 30 31 //#if 0 32 //使用系统的方法进行归档 NSKeyedArchiver 33 NSDictionary *mutDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"111",@"one",@"222",@"two", nil]; 34 [mutDic setValue:[NSDate date] forKey:@"date"]; 35 [mutDic setValue:[NSNumber numberWithInteger:8889] forKey:@"num"]; 36 [mutDic setValue:[@"ios" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"]; 37 38 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mutDic]; 39 BOOL ret = [data writeToFile:@"/Users/qianfeng/Desktop/day21-OC11_json_归档和解归档类/day21_OC11_归档与反归档/lpj.plist" atomically:YES]; 40 if (ret) { 41 NSLog(@"归档成功"); 42 } 43 else{ 44 NSLog(@"归档失败"); 45 } 46 47 //#endif 48 49 #if 0 50 //解归档 51 NSData *data = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Desktop/day21-OC11_json_归档和解归档类/day21_OC11_归档与反归档/lpj.plist"]; 52 //NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/qianfeng/Desktop/day21-OC11_时间类/day21_OC11_归档与反归档/data.txt"]; 53 NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 54 if (dict) { 55 NSLog(@"解归档成功"); 56 NSLog(@"%@",dict); 57 }else{ 58 NSLog(@"解归档失败"); 59 60 } 61 #endif 62 63 #if 0 64 // NSSearchPathForDirectoriesInDomains 这个主要就是返回一个绝对路径用来存放我们需要储存的文件。 65 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 66 NSString *documentsDirectory = [paths objectAtIndex:0]; 67 NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"shoppingCar.plist"]; 68 #endif 69 70 71 } 72 73 74 @autoreleasepool { 75 76 #if 0 77 78 //手动归档 - (instancetype)initForWritingWithMutableData:(NSMutableData *)data; 79 80 //创建一个可变的数据对象 81 NSMutableData *mulData = [[NSMutableData alloc] init]; 82 83 84 NSKeyedArchiver *keyArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mulData]; 85 //数组对象 86 NSArray *array = [NSArray arrayWithObjects:@"one",[NSNumber numberWithInteger:1111],[NSDate date],[@"i love ios" dataUsingEncoding:NSUTF8StringEncoding], nil]; 87 //字典对象 88 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"111",@"one",array,@"array",[NSNumber numberWithInt:1212],@"num",[NSDate date],@"date", nil]; 89 90 //对数组和字典进行编码 原理:其实是是将数组array和字典dict对象写入到可变数据对象mulData中。之后在利用muData的writeToFile将二进制数据写入到文档中 91 [keyArchiver encodeObject:array forKey:@"array"]; 92 [keyArchiver encodeObject:dict forKey:@"dict"]; 93 94 //编码结束一定需要调用finishEncoding方法 95 [keyArchiver finishEncoding]; 96 97 //写入文件 98 BOOL flag = [mulData writeToFile:@"/Users/qianfeng/Desktop/day21-OC11_时间类/day21_OC11_归档与反归档/initForWritingWithMutableData.txt" atomically:YES]; 99 if (flag) { 100 NSLog(@"归档成功"); 101 }else{ 102 NSLog(@"归档失败"); 103 } 104 105 #endif 106 107 #if 0 108 //手动解归档 109 //过程 1.首先创建一个NSData对象,并读取文件的内容 2.通过NSKeyedUnarchiver 的 initForReadingWithData方法从数据对象中解归档对象 3.通过关键字从解归档对象中获取数组对象和字典对象 110 NSData *data = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Desktop/day21-OC11_时间类/day21_OC11_归档与反归档/initForWritingWithMutableData.txt"]; 111 NSKeyedUnarchiver *keyUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 112 113 //通过关键字 解码数组 114 NSArray *array = [keyUnarchiver decodeObjectForKey:@"array"]; 115 //通过关键字 解码字典 116 NSDictionary *dict = [keyUnarchiver decodeObjectForKey:@"dict"]; 117 118 [keyUnarchiver finishDecoding]; 119 120 NSLog(@"array = %@",array); 121 NSLog(@"dict = %@",dict); 122 123 #endif 124 } 125 return 0; 126 }
归档:将程序中的对象写入到文件的过程
解归档:将文件中的数据读取为程序中的对象的过程
标签:
原文地址:http://www.cnblogs.com/BeyondAverage0908/p/4596798.html