使用NSKeyedArchiver存储自定义数据模型 [英] Using NSKeyedArchiver to store custom data model

查看:151
本文介绍了使用NSKeyedArchiver存储自定义数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rss阅读器。它只是一个tableview,每个单元格显示一个自定义数据模型RSSEntry。我有一个NSMutableArray allEntries,它包含我从服务器获得的所有RSSEntry。这就是RSSEntry的样子:

I'm working on a rss reader. It is just a tableview and each cell shows a custom data model RSSEntry. And I have a NSMutableArray allEntries which contains all RSSEntry I got from the server. This is what RSSEntry looks like:

    @interface RSSEntry : NSObject <NSCoding> {
        NSString *_blogTitle;
        NSString *_articleTitle;
        NSString *_articleUrl;
        NSDate *_articleDate;
    }

我想从本地存档恢复数据,所以当我退出并打开这个应用程序时再次,tableview在上次刷新时填充了数据。

I want to restore data from local archive so when I quit and open this app again, the tableview is populated with data when last refresh.

我已经阅读了开始iOS5编程中的示例,但它只存储和恢复一个数据模型。所以我不知道如何存储这个充满我的自定义数据模型的可变数组并将其恢复。

I've read example in 'Beginning iOS5 Programming', but it only store and restore only one data model. So I don't know how to store this mutable array full of my custom data model and restore it.

我使用以下代码来存储和恢复数据:

I use following code to store and restore data:

// STORE
NSMutableData *data = [[NSMutableData alloc] init]; 
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:entry forKey:kDataKey];
[archiver finishEncoding];

// RESTORE
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath:kFilename]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
RSSEntry *entries = [unarchiver decodeObjectForKey:kDataKey];
[unarchiver finishDecoding];

我只能以这种方式存储和恢复一个数据模型。

I can only store and restore one data model in this way.

谁能帮助我。我是iOS编程的新手,也许它有点沉闷。来自。

Who can give me a hand. I'm new to iOS programming, maybe it's a little dull .sorry.

推荐答案

将您要存储的所有条目放入 NSArray 并使用此代码:

Place all the entries you want to store into an NSArray and use this code:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:entries];

其中条目是您的条目数组。很简单,只需一行代码。

Where entries is your array of entries. Nice and easy, just one line of code.

要取消归档,只需使用

NSArray *entries = [NSKeyedUnarchiver unarchiveObjectWithData:data];

NSKeyedArchiver 能够存档任何数据结构一个非常复杂的Objective-C对象实现 NSCoding 协议,包括嵌套的 NSArray s, NSDictionary s和自定义对象到文件。 NSKeyedUnarchiver 执行相反的操作,获取 NSKeyedArchiver 生成的任何数据,并重新建立已归档的原始对象图。从技术上讲,它们可能只是一个单独的类,但Apple决定将它们按功能分开,因为这在语法上更正确或者是某些东西:)

NSKeyedArchiver is able to archive any data structure, even a very complex one, of Objective-C objects that implement the NSCoding protocol, including nested NSArrays, NSDictionarys, and custom objects, to a file. NSKeyedUnarchiver does the reverse, taking any data produced by NSKeyedArchiver and reestablishing the original object graph that was archived. Technically, they could just be a single class, but Apple decided to separate them by functionality because this is more grammatically correct or something :)

我假设你能写生成的 NSData 对象到文件,然后再读回来。

I assume you are able to write the resulting NSData object to a file, and read it back again.

这篇关于使用NSKeyedArchiver存储自定义数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆