0

I have a large number of nested dictionaries and the leaf (or most nested) dictionaries store integer data and integer keys. All the information remains constant (but may change in a future release). I am currently allocating the dictionaries from constants in code but I feel I should be reading that information from XML or similar. I have read about Core information, plists, databases and archives but I don't want the user to be able to change it, I never want to be able to write it (except maybe during the release procedure) and I never want to display it. I would like to be able to hand edit it before release.

What is the best method to store this constant data?

Ant
  • 1,668
  • 2
  • 18
  • 35
  • Save the NSDictionary as a plist file: [http://stackoverflow.com/questions/6311037/save-nsdictionary-to-plist][1] [1]: http://stackoverflow.com/questions/6311037/save-nsdictionary-to-plist – hukir Oct 30 '12 at 14:52
  • I forgot to mention, I have a number of small classes that contain say 2 ints and a set, that are also in the hierarchy. Can I use plists for that too? – Ant Oct 30 '12 at 14:54
  • Ah, then it wouldn't work. Documentation says items must be instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary. You would look into using NSCoder to encode and decode the dictionary. I haven't used it myself, but I think you would just have to implement the handling in your classes. – hukir Oct 30 '12 at 14:58
  • The `NSCoder` protocol is very simple to implement. Basically you'll just need to encode your `int` into `NSNumber` objects. – Michael Kernahan Oct 30 '12 at 15:11
  • But is this the _best_ method? Hukir, you said you hadn't used it and @Michael does this work with my small classes? – Ant Oct 30 '12 at 16:28
  • It's the easiest. Apple has `NSKeyedArchiver` (and unarchiver) which will just write the full object structure to the file system and read it back in again. – Michael Kernahan Oct 30 '12 at 17:28
  • Sorry @MichaelKernahan I'm confused now. Are you saying I should use NSCoder or NSKeyedArchiver? – Ant Oct 30 '12 at 17:53
  • Sorry I wasn't clear. To use the `NSKeyedArchiver` your object (and all sub objects for arrays/dictionaries) must implement `NSCoding` – Michael Kernahan Oct 30 '12 at 18:22

1 Answers1

0

Basically you need to ship your data in files with the app - XML or JSON are both suitable for this. When I have had to do something similar I used JSON

It works something like this :

Define your JSON in text file (UTF8) and then use the

NSString initWithContentsOfFile to load file contents into a NSString

You can then use the NSJSONSerialization JSONObjectWithData to give you the top level dictionary for your JSON

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions  error:&error];

From this you can extract your NSStrings / NSArrays using NSDictionary objectForKey for your data. Obviously the exact format will depend on your JSON format

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
gheese
  • 1,170
  • 1
  • 11
  • 17
  • Thank you for your answer. I don't know JSON, I thought it was for the layout of webpages. Is it worth learning for this one task? You said or XML, but there seem to be many methods which use an underlying XML format. Maybe my question should have been which is the _best_ and why? – Ant Oct 30 '12 at 16:31
  • JSON was originally developed for passing to a javascript eval function but it can also be used to describe data in much the same way as XML without the overhead. IOS provides JSON support via the JSON framework, it is probably suitable for you because you mention a large number of nested dictionaries. Check out this tutorial http://www.raywenderlich.com/5492/working-with-json-in-ios-5 – gheese Nov 01 '12 at 10:28
  • I've marked this as the answer with the caveat that you need to learn JSON. I'm not completely convinced that there is a _best_ answer. – Ant Nov 08 '12 at 09:21