归档(archive)文件(二)
2015-12-22 15:40
218 查看
http://blog.sina.com.cn/s/blog_7b9d64af01019kw8.html
可以参见我的第一篇关于归档的说明。
归档(archive)文件(一)
考虑如下情况:
1.我们有很多不同的对象需要归档,并且对象之间可能相差很大,每次进行归档麻烦,效率也不高。
2.被归档的对象不确定有多少个。
3.希望将不同对象归档到一个文件中,易于操作。
所以,如果我们能够全部将需要归档的对象首先存放在一个空间中,然后,一次性归档。这样做,那就方便多了!
这就是今天的主角登场!使用NSData和NSMutableData来进行归档!
由于,前面篇幅的说明,只需要贴代码了:
NSMutableData *dataArear=[NSMutableData data];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:dataArear];
[archiver encodeObject:objArray forKey:@"AddressCard_Array"];
[archiver encodeObject:card1 forKey:@"AddressCard_SingleObj"];
[archiver finishEncoding];
[archiver release];// 注意释放
BOOL isOk=[dataArear writeToFile:filePhyName atomically:YES];
if (isOk) {
NSLog(@"Success!");
}else{
NSLog(@"False!");
}
NSMutableData *theData=[NSMutableData dataWithContentsOfFile:filePhyName];
NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:theData];
NSMutableArray *cardArray=[unarchiver decodeObjectForKey:@"AddressCard_Array"];
AddressCard *theCard=[unarchiver decodeObjectForKey:@"AddressCard_SingleObj"];
[unarchiver finishDecoding];
[unarchiver release];// 注意释放
for (AddressCard *theCard in cardArray) {
[theCard print];
}
[theCard print];
希望对您有所帮助!
可以参见我的第一篇关于归档的说明。
归档(archive)文件(一)
考虑如下情况:
1.我们有很多不同的对象需要归档,并且对象之间可能相差很大,每次进行归档麻烦,效率也不高。
2.被归档的对象不确定有多少个。
3.希望将不同对象归档到一个文件中,易于操作。
所以,如果我们能够全部将需要归档的对象首先存放在一个空间中,然后,一次性归档。这样做,那就方便多了!
这就是今天的主角登场!使用NSData和NSMutableData来进行归档!
由于,前面篇幅的说明,只需要贴代码了:
NSMutableData *dataArear=[NSMutableData data];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:dataArear];
[archiver encodeObject:objArray forKey:@"AddressCard_Array"];
[archiver encodeObject:card1 forKey:@"AddressCard_SingleObj"];
[archiver finishEncoding];
[archiver release];// 注意释放
BOOL isOk=[dataArear writeToFile:filePhyName atomically:YES];
if (isOk) {
NSLog(@"Success!");
}else{
NSLog(@"False!");
}
NSMutableData *theData=[NSMutableData dataWithContentsOfFile:filePhyName];
NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:theData];
NSMutableArray *cardArray=[unarchiver decodeObjectForKey:@"AddressCard_Array"];
AddressCard *theCard=[unarchiver decodeObjectForKey:@"AddressCard_SingleObj"];
[unarchiver finishDecoding];
[unarchiver release];// 注意释放
for (AddressCard *theCard in cardArray) {
[theCard print];
}
[theCard print];
希望对您有所帮助!
相关文章推荐
- Github 使用小结
- 自定义AlertDialog(仿真融宝)
- iOS证书及ipa包重签名探究
- Android初学习 - 分析应用程序组件Activity的"singleTask"
- Android中有哪些好的开发框架?
- window 7 下用MyEclipse编写Java程序连接HBase时遇到的问题
- error while loading shared libraries: libpcap.so.1 解决方法
- bytes[i] & 0xf0是怎么运算的
- 【总结】输入法弹窗的使用
- Objective-C 关键字:retain, assgin, copy, readonly,atomic,nonatomic
- 【HDOJ】1857 Word Puzzle
- WordPress中使主题支持小工具以及添加插件启用函数
- Maven构建SpringMVC项目详解
- 用NSCoding协议完成“编码/解码”操作-Object-C
- [置顶] 一步步构建大型网站架构
- [置顶] 一步步构建大型网站架构
- [置顶] 一步步构建大型网站架构
- 企业证书发布
- LeetCode(93) Restore IP Addresses
- 一步步构建大型网站架构