问题描述
我有一个对象的字典;它们都是应该可序列化的POCO对象。我应该看看把这些写到磁盘的什么技术。我正在寻找最简单的选项来写几个列表来保存状态。
I have a dictionary of objects; they are all POCO objects that should be serializable. What technique should I look at for writing these to disk. I'm looking for the simplest option to write a few lists to save state.
我想我有3个选项。
-
plist文件。但是,这似乎只限于存储预定义对象(字符串,数字等)而不是对象(如名字和年龄的人)。
plist files. However this seems to be limited to only storing predefined objects (strings, numbers etc) not objects (like a person with a name and age).
CoreData。 (新的3.0)这将工作很好;然而,我的数据模型将需要改变,使这项工作。这将是一个大量的返工,我不确定是否值得的努力。
CoreData. (New in 3.0) This would work well; however my data model would need to change to make this work. This would be a massive rework and I'm not sure if it is worth the effort.
SQLLite。实现一个简单的SQL数据库读取和读取。我已经尽了最少的努力,但我不想重写一些核心数据ORM函数。
SQLLite. Implement a simple SQL database to read to and from. I have done the least amount of reserch into this one, but I don't want to have to 'rewrite' some of the core data ORM functions.
推荐答案
这样做与在Mac OS X上的做法相同:POCOs必须符合 NSCoding
协议。有关概念参考,请参见,和为NSCoding引用。
You do this in the same way you'd do it on Mac OS X: your POCOs must conform to the NSCoding
protocol. See here for a conceptual reference, and here for the NSCoding reference.
如果数据不是那么疯狂广泛,你没有可笑的复杂的对象之间的关系,写一切作为plist可能是你最好的选择;它在代码中执行和简单实现非常快。像你说的,CoreData可能是一个额外的工作来适应你的代码,并且sqlite真的只是存储数据是完美的存储在关系数据库。请记住,sqlite也比使用二进制列表更慢,使用更多的资源。
If the data isn't that crazy extensive and you don't have ridiculously complicated relationships between your objects, writing everything out as a plist is probably your best option; it's very fast to execute and simple to implement in your code. Like you said, CoreData will probably be a lot of extra work to adapt your code to, and sqlite really is only good for storing data that's perfect to be stored in a relational database. Keep in mind that sqlite is also slower and uses more resources than working with binary plists.
这篇关于如何在iPhone sdk中序列化一个简单的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!