简单示例归档与解档的过程
2015-12-12 22:00
453 查看
演示将一个字典类型归档到文件和从文件解档的过程。
// // 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; }
相关文章推荐
- 无法按8扩展段(在还原表空间‘XXXX’中)
- 南阳OJ 16 矩形嵌套
- 自我总结(四) ---java web项目完结,j2ee的开始
- leetcode笔记:Spiral Matrix
- XSS quiz 1~5解题方案
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 解决方法
- 高维度下的数据科学—线性空间(上)
- android - Searchable Configuration
- LeetCode Game of Life(位操作)
- C++ 编译环境,字体颜色设置
- Android Studio运行时出现多个项目
- 关于添加-objc后缀的解释
- Java反射机制
- P02 (*) 获取列表的倒数第二个元素
- P02 (*) 获取列表的倒数第二个元素
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- pc网页到移动端怎么自动加载适应移动端的css。
- 本来已经拥有
- JavaFX 内置的布局面板
- 简单的在两个activity中传递数据