如何使用NSKeyedArchiever归档两个对象? [英] How do I archive two objects using NSKeyedArchiever?

查看:60
本文介绍了如何使用NSKeyedArchiever归档两个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我有一个数组,在其中使用NSKeyedArchiver进行归档.

Previously, I have an array in which I use NSKeyedArchiver to archieve.

[NSKeyedArchiver archiveRootObject:array toFile:docPath];

调用了 applicationWillTerminate applicationDidEnterBackground .

启动后,在 didFinishLaunchingWithOPtions 中,未对阵列进行归档:

Upon starting up, in didFinishLaunchingWithOPtions, the array is unarchieved:

NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];

一切都很好.从那时起,我为Settings变量添加了Singleton类.我也想存档Singleton.我该怎么办?

Everything was good. Since then, I added a Singleton class for Settings variables. I would like to archive the Singleton as well. How would I do that?

让我感到困惑的部分是,如果我拨打相同的消息:

The part that confuses me is if I call the same message:

[NSKeyedArchiver archiveRootObject:singleton toFile:docPath];

应用程序如何在调用时知道我要取消存档的对象?

How can the app know which object I want to unarchive when I call:

[NSKeyedUnarchiver unarchiveObjectWithFile:docPath];

是数组还是单例?

archiveRootObject接受一个(id)对我来说意味着我想要的任何东西.我可以创建一个数据类,其中包括数组和单例.这就是我应该做的吗?有没有更好的办法?欢迎任何建议.

The archiveRootObject accepts an (id) which to me means whatever I want. I can create a data class which includes the array and the singleton. Is that how I am suppose to do this? Is there a better way? Any suggestions are welcomed.

谢谢!

推荐答案

如果要在根级别编码多个对象,则需要使用 + alloc -initForWritingWithMutableData:自己,将其发送给-encodeObject:forKey:有关要添加到存档中的每个对象的消息,最后发送给 -finishEncoding .然后,您可以自己将数据写入文件.

If you want to encode more than one object at the root level, you'll need to create the archiver using +alloc and -initForWritingWithMutableData: yourself, send it -encodeObject:forKey: messages for each of the objects you want to add to the archive, and finally -finishEncoding. You can then write the data to a file yourself.

它看起来像这样(警告:前面未经测试的代码):

It'll look something like this (warning: untested code ahead):

NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

[archiver encodeObject:someObject forKey:@"people"];
[archiver encodeObject:anotherObject forKey:@"places"];
[archiver encodeObject:thirdObject forKey:@"things"];
[archiver finishEncoding];

[data writeToURL:someUrl atomically:YES];
[archiver release];

要检索对象,您将执行实质上相反的操作:将文件读入NSData对象;使用 + alloc -initForReadingWithData:创建一个NSKeyedUnarchiver;使用 -decodeObjectForKey:检索您关心的对象;最后调用 -finishDecoding .您几乎可以从上到下阅读上面的示例,并进行了一些名称更改.

To retrieve the objects, you'll do essentially the opposite: read a file into an NSData object; create an NSKeyedUnarchiver with +alloc and -initForReadingWithData:; retrieve the objects you care about using -decodeObjectForKey:; and finally call -finishDecoding. You can almost read the sample above from bottom to top with a few name changes.

这篇关于如何使用NSKeyedArchiever归档两个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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