views:

46

answers:

2

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!

+3  A: 

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..

Jack
Ok. I was thinking about Core Data for a while, and it seemed rather complicated, and I wasn't sure whether to use it... but now I'll use it for sure. Thanks for answering my question!
Nano8Blazex
+1 for this use-case, Core Data is probably the way to go.
Dave DeLong
+3  A: 

Core Data is certainly an excellent option, as @Jack has shown. There are some other options as well.

  1. NSCoding - You can make your model objects conform to the NSCoding protocol (similar to java.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.

  2. 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.

  3. NSUserDefaults - if your data is very small and is just essentially key-value pairs, then you can probably throw it all into the NSUserDefaults 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.

Dave DeLong
Wow, thanks. I've never even heard of half these before. Also: In Core Data, is it possible to "export" the data into external files? (for other applications to "import" it?) Or is it already in an exportable state? Just wondering.
Nano8Blazex
@Nano8Blazex any importation or exportation has to be done by you, in code.
Dave DeLong
I see. Well, as long as it's possible, it's good! Thanks.
Nano8Blazex