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 an开发者_开发知识库swers? Core Data?
Thanks!
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:
- ORM between databases and run-time objects
- persistence
- automatic building tools (like an ER-editor)
- it's ready out of the box, you won't need to implement almost anything.. you will already have access to your data by just querying it to the object controllers
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..
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.
精彩评论