iPhone_开发_基本术语_Serialization

来源:互联网 发布:java怎么学 编辑:程序博客网 时间:2024/05/16 17:25

Serialization converts Objective-C types to and from an architecture-independent byte stream.

这句话中的
Serialization 有两个意思:序列化 与 反序列化。
序列化 是 将 Objective-C 数据类型 转化 为 体系结构无关的字节流。
反序列化 是 序列化 的反向操作:将 体系结构无关的字节流 转化 为 Objective-C 数据类型。

本 sample 以 NSArray 类为例,进行说明
步骤:
新建工程 命令行 模板
添加以下代码
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/data.txt"];

NSLog(@"%@",documentsDirectory);
NSArray *arr = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
[arr writeToFile:documentsDirectory atomically:YES;
NSArray *arr2 = [NSArray arrayWithContentsOfFile:documentsDirectory];
NSLog(@"%@",arr2);

运行程序
在 文档 (~/Documents) 下,将生成 data.txt 文件
注意:若某位童鞋的 Documents 文件夹下 恰好已经存在 data.txt 文件,请自行解决 :)

Ref:

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Archiving/Articles/serializations.html#//apple_ref/doc/uid/20000947-BCIEBEGI

原创粉丝点击