On Jun 16, 2009, at 11:25 AM, Andy Klepack wrote:

I'm curious about what patterns exist for binding to nested sets of preferences such as a dictionary value at the top level of the user defaults. I've seen a couple of questions similar to this, but no one appears to have received the definitive answer.

My scenario is that I have an application with several distinct modules and I would like each module's preferences to be stored in a separate dictionary.

Say the user defaults look like so:
{
 module = {
    moduleValue = "xyz"
 }
}

The module has a nib with a text field that should be bound to the moduleValue. I'd like to have it bind to sharedUserDefaultsController.values.module.moduleValue. As far as I can tell this is not valid. The bindings system thinks that 'module.moduleValue' is the key in the user defaults to bind to.

This is almost certainly not the case. What makes you think it is?

My second approach was to create an NSObjectController for which the content was bound to sharedUserDefaultsController.values.module. I then bound the text field to objectController.moduleValue. In this case there were problems when the module dictionary did not already exist (the object controller's content was null), plus whenever the value moduleValue was set it didn't trigger any user defaults behavior since it was a modification of nested content and thus didn't trigger change notification for defaults themselves.

The most obvious solution is to create a custom object (ModuleController) for the nib, create a moduleValue property with custom accessors, implement those accessors to interact with the user defaults, and then bind the text field to those properties. At the same time, that seems pretty code intensive for something I suspect is a fairly common design.


The issue is usually not about nested structures, but about the fact the plist backing of user defaults does not use mutable objects. Therefore, all of your dictionaries are immutable NSDictionary's. Unfortunately the NSUserDefualtsController isn't smart enough to deal with nested structures, so you have to provide your own intermediary. So, this last approach is really (AFAIK) the common pattern for dealing with nested structures on user defaults.

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to