现在的位置: 首页 > 综合 > 正文

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

2013年10月22日 ⁄ 综合 ⁄ 共 1225字 ⁄ 字号 评论关闭

主要做一下汇总。

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了

抱歉!评论已关闭.