简单示例归档与解档的过程
来源:互联网 发布:指南针软件 不断打电话 编辑:程序博客网 时间:2024/06/05 06:29
演示将一个字典类型归档到文件和从文件解档的过程。
//// main.m/* 演示将一个字典类型进行归档和解档的过程 */#define PATH @"/Users/apple/Documents/ios_dev/test_case/TestNSArchive/dic.plist"#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { /* 归档:将数据写入文件 */ NSDictionary *dic = @{@"key1":@"value1", @"key2":@"value2", @"key3":@"value3"}; /* archivedDataWithRootObject:Returns a data object containing the encoded form of the object graph whose root object is given. */ NSData *data = [NSArchiver archivedDataWithRootObject:dic]; /* NSDictionary->NSData */ /* writeToFile:Writes the bytes in the receiver to the file specified by a given path. */ BOOL ret = [data writeToFile:PATH atomically: YES]; if (ret == 0) { NSLog(@"write fail."); return 0; } /* 解档:方法1 */ /* unarchiveObjectWithFile:Decodes and returns the object archived in the file path.*/ id obj; obj = [NSUnarchiver unarchiveObjectWithFile:PATH]; /* path->id */ NSLog(@"first:%@",obj); /* 解档:方法2 */ /* unarchiveObjectWithData:Decodes and returns the object archived in a given NSData object.*/ NSData *data2 = [NSData dataWithContentsOfFile:PATH]; /* path->NSData */ obj = [NSUnarchiver unarchiveObjectWithData:data2]; /* NSData->id */ NSLog(@"second:%@", obj); } return 0;}输出结果:
2015-12-12 21:59:03.075 TestNSArchive[510:15924] first:{ key1 = value1; key2 = value2; key3 = value3;}2015-12-12 21:59:03.076 TestNSArchive[510:15924] second:{ key1 = value1; key2 = value2; key3 = value3;}
0 0
- 简单示例归档与解档的过程
- Linux的归档与解归档
- 数据的归档与解归档
- 解档与归档
- 归档与解档
- 归档与解归档
- 归档与解归档
- 归档与解归档
- 归档与解归档
- iOS的归档与解档
- IOS 对象的归档与解档
- iOS对象的归档与解档
- OC中(简单)自定义时间|操作文件部分一(文件的拷贝|归档与解归档)
- IOS_归档与解档
- XMG 解档与归档
- 简单存储过程示例
- Oracle 归档与非归档的切换
- Oracle 归档与非归档的切换
- XSS quiz 1~5解题方案
- 无法按8扩展段(在还原表空间‘XXXX’中)
- mysql的类型问题
- [javase学习笔记]-3.5 for循环的嵌套
- hdoj GTW likes math 5595 (暴力)
- 简单示例归档与解档的过程
- MyEclipse的项目文件导入到Eclipse
- 一些兼容性问题
- jQuery之家的资源
- MyEclipse中设置文件的默认打开方式
- 不好的MySQL过程编写习惯
- C语言预处理及特殊函数介绍(extern/exit/atexit/volatile/static/calloc/realloc等)PART TWO
- hdoj GTW likes gt 5596 (DP机智转换)好题
- hdu 5596 GTW likes gt