Re: Should I use NSDocument, NSUserDefaults or something else
Re: Should I use NSDocument, NSUserDefaults or something else
- Subject: Re: Should I use NSDocument, NSUserDefaults or something else
- From: Angus Hardie <email@hidden>
- Date: Thu, 30 Sep 2010 14:01:23 +0100
On 30 Sep 2010, at 12:42, Jonny Taylor wrote:
> My app is a workstation for a type of microscope, which from the point of view of the software consists of several different video cameras and some other devices controlled over USB. Each camera has associated settings (e.g. exposure) that I want to persist between app launches. I am not sure what the best way is of storing this information. There are multiple cameras involved, with each camera having quite a distinct role, so I would like to tie the settings to individual camera serial numbers.
>
> I had a look into NSUserDefaults but that doesn't sound quite ideal. As far as I can see (and I may be missing something) it is not inherently designed for hierarchical data storage. If I wanted to store, say, QI1438.exposure and PS1451.exposure as separate (flat) keys, then I don't think there would be a way of specifying a default value for the generalized key "exposure" in registerDefaults (regardless of the camera serial number). It could also be argued that these settings are not really "user preferences", particularly since one could imagine having a different bank of settings for different experiments.
>
> This made me think that an NSDocument might be a more appropriate solution (different document for each experiment, storing a hierarchical dictionary of key/value pairs but implementing my own handling of defaults for missing keys, etc). However this class is understandably a very complex class and I'm finding it hard to drill down to the details of what I would need to do in order to implement this pretty basic functionality - particularly since in some ways it would be a non-standard use of the class, in that the "document" was not associated with one single window, etc. Can anybody point me towards some example code that uses an NSDocument for this sort of application?
One point to note is that you can store an NSDictionary or an NSArray as a value in NSUserDefaults.
That dictionary or array could store other dictionaries or arrays, which gives you a hierarchy.
Alternatively you could look at serializing the data to a plist file. If you have it in memory as an NSDictionary or NSArray then serializing to a plist is fairly simple
writeToURL:atomically: to write the dictionary to disk, dictionaryWithContentsOfURL: or initWithContentsOfURL: to read it in.
This is a bit more complicated than user defaults, but it keeps your data outside the defaults system (if that's what you want)
I would suggest that NSDocument might be more than you need.
It's more orientated towards user managed documents (e.g TextEdit) , while your app sounds more like something that manages the data itself. (More like iTunes)
Unless you intend the user to have sessions of usage and somehow switch between them, or distribute them in some way, which might be a reason to use NSDocument.
Angus
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden