数据存储问题
另一个新手问题:在用 Obj-C 编写的 Cocoa 应用程序中存储数据的最佳方式是什么?例如,如果我想创建一种“测验器”,用预先编写的(和用户编写的)问题来测验用户?我将如何存储这些问题和答案?核心数据?
谢谢!
Another newbie question: What's the best way to store data in a Cocoa application written in Obj-C? For example if I want to create a sort of "quizzer" that quizzes the user with pre-written (and user-written) questions? How would I store these questions and answers? Core Data?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然是核心数据!
它将处理所有事情..看看这里: http://developer.apple.com/macosx /coredata.html
这是一个完整的 API,可以处理:
可能这个解决方案对于您的问题来说太大了,但您将学习如何在简单的情况下使用它,我将来会派上用场..
Of course it's Core Data!
It will handle everything.. take a look here: http://developer.apple.com/macosx/coredata.html
It's a full API that can handle:
Probably this solution is over-sized for your problem but you will learn how to use it with a simple case, and I will come handy in the future..
正如@Jack 所表明的那样,Core Data 无疑是一个很好的选择。还有一些其他选择。
NSCoding
- 您可以使模型对象符合 NSCoding 协议(类似于java.io.Serialized
),这意味着您可以直接编写将它们保存到文件中。我发现,当我没有大量数据需要保存,并且我保存的数据具有相对简单的结构时,这是一个很好的选择。SQLite - 如果您的数据关系密切,您可能需要考虑直接使用数据库(可能是 SQLite)。 Core Data 是一个对象存储,虽然它处理对象之间的关系之类的事情,但它不允许您做真正有用的事情,例如 INNER/LEFT/OUTER/CROSS/NATURAL JOIN 或其他多连接表运算符。
NSUserDefaults
- 如果您的数据非常小并且本质上只是键值对,那么您可以将其全部放入NSUserDefaults
对象中,该对象将持久化它在首选项文件中为您提供。但是,即使您的数据很简单,如果数据量很大,NSUserDefaults
也可能不是最佳选择。Core Data is certainly an excellent option, as @Jack has shown. There are some other options as well.
NSCoding
- You can make your model objects conform to the NSCoding protocol (similar tojava.io.Serializable
), which means you'd be able to directly write them to files. I've found that this is a great option when I don't have massive amounts of data to persist, and the data that I am persisting has a relatively simple structure.SQLite - If your data is very relational, you may want to consider using a database (probably SQLite) directly. Core Data is an object store, and while it handles things like relationships between objects, it doesn't allow you to do really useful things like
INNER/LEFT/OUTER/CROSS/NATURAL JOIN
or other multi-table operators.NSUserDefaults
- if your data is very small and is just essentially key-value pairs, then you can probably throw it all into theNSUserDefaults
object, which will persist it for you in the preferences file. However, even if your data is simple,NSUserDefaults
might not be the best option if you have lots of it.