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>,

@interface UploadItem : NSObject<NSCoding> {
	FileOrigin type;
	NSString *filepath;
    VideoInfo *video;
}

实现两个方法


- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject: self.filepath forKey:@"uploaditem_filepath"];
    [aCoder encodeObject: self.video forKey:@"uploaditem_video"];
    [aCoder encodeInt:self.type forKey:@"uploaditem_type"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    [self init];
    self.filepath = [aDecoder decodeObjectForKey:@"uploaditem_filepath"];
    self.video = [aDecoder decodeObjectForKey:@"uploaditem_video"];
    self.type = (FileOrigin)[aDecoder decodeIntForKey:@"uploaditem_type"];
    return self;
}


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

UploadItem,那么我直接调用

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

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

读取的时候

    self.array = [aDecoder decodeObjectForKey:@"uploadprocess_array"];
就ok了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值