ios iphone 如何序列化存储,保存数据。

主要做一下汇总。

1。首先还是官方文档Archives and Serializations Programming Guide

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Archiving/Articles/creating.html#//apple_ref/doc/uid/20000949-BABGBHCA

2。深入理解iPhone数据持久化(手把手教你iphone开发 - 基础篇)

http://blog.csdn.net/dongfengsun/article/details/4799249

3.如何保存游戏数据

http://hi.baidu.com/shadowstudios/blog/item/49186df160dbf628bc31090c.html



说说对象归档NSKeyedArchiverNSKeyedUnarchiver的存储。

比如我要存储

UploadItem,那么它必须继承自<NSCoding>,

  1. @interface UploadItem : NSObject<NSCoding> {  
  2.     FileOrigin type;  
  3.     NSString *filepath;  
  4.     VideoInfo *video;  
  5. }  

实现两个方法


  1. - (void)encodeWithCoder:(NSCoder *)aCoder  
  2. {  
  3.     [aCoder encodeObject: self.filepath forKey:@"uploaditem_filepath"];  
  4.     [aCoder encodeObject: self.video forKey:@"uploaditem_video"];  
  5.     [aCoder encodeInt:self.type forKey:@"uploaditem_type"];  
  6. }  
  7.   
  8. - (id)initWithCoder:(NSCoder *)aDecoder  
  9. {  
  10.     [self init];  
  11.     self.filepath = [aDecoder decodeObjectForKey:@"uploaditem_filepath"];  
  12.     self.video = [aDecoder decodeObjectForKey:@"uploaditem_video"];  
  13.     self.type = (FileOrigin)[aDecoder decodeIntForKey:@"uploaditem_type"];  
  14.     return self;  
  15. }  


这样比如外部有一个NSMutableArray* array存储的是

UploadItem,那么我直接调用

  1. [aCoder encodeObject: self.array forKey:@"uploadprocess_array"];  

可以存储整个UploadItem类的数组。

读取的时候

  1. <span style="font-size:16px;">    self.array = [aDecoder decodeObjectForKey:@"uploadprocess_array"];</span>  
就ok了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值