Rick --

I pulled my hair out over this one, but I finally found an acceptable workaround, although it doesn't solve the fundamental problem of NSDictionaryController.

What I ended up doing is setting the whole outer dictionary (the one controlled by the NSDictionaryController) via a KVO-compliant method. This sucks because it means that you'll have to manually write methods for the actions of your UI controls instead of relying on bindings to do it for you. But it *does* get around the paradox of either being unable to set properties of an individual object in the dictionary or violating KVO-compliance.

So, for example, in my project, my NSDictionaryController is bound to the entriesDict of the selected EPWeblog object in my master-detail interface. When I want to change one of the objects in the dictionary, I do the following:

NSMutableDictionary *tempMutDict = [NSMutableDictionary dictionaryWithDictionary:[weblog entriesDict]];
[tempMutDict setObject:dictionaryValue forKey:theExistingKey];
[weblog setEntriesDict:tempMutDict];

entriesDict is a property of the EPWeblog object (the variable 'weblog'), and it's KVO-compliant via the entriesDict and setEntriesDict: methods. So I simply copy the outer dictionary, change the one object in the dictionary (represented by the new dictionaryValue variable), and then set the outer dictionary via the setEntriesDict: method. If you call addObject: to your NSDictionaryController, it does precisely the same thing -- it adds a new object and sets the whole outer dictionary via the appropriate KVO- compliant method.

After all this, I too am planning to file a bug on NSDictionaryController because this is pretty ridiculous for something that should be straightforward. I did a bunch of subclassing to figure out exactly what NSDictionaryController was doing. Even if the dictionary it's controlling contains mutable values inside its objects, NSDictionaryController represents them as immutable objects. It's infuriating.

It does make me feel better that someone else is having this same problem, though. :)

-- Simone



Il giorno 2009-01-06, alle ore 14:06, Rick Hoge ha scritto:


I'm using an NSDictionaryController object to control the data provided to an NSTableView object. I'm attempting to use key-value coding through the dictionary controller to manipulate the data in a bindings-compatible way, by calling the method -arrangedObjects on the NSDictionaryController, and then manipulating the NSDictionaryControllerKeyValuePairs that this method returns in an array.

Any luck?

I am having a similar problem - mutable dictionary objects that are themselves stored in a dictionary will be presented as immutable dictionaries by the NSDictionaryController. This makes it impossible to bind controls to keypaths that drill into dictionaries below the level of the outermost container.

i.e. even if arrangedObjects.value should point to a mutable dictionary, attempts to edit arrangedObjects.value.someKey via a UI control will generate the following error:

*** -[NSCFDictionary setObject:forKey:]: mutating method sent to immutable object

Having a workaround for this would be really nice - has anyone found one? I will file a bug as I don't think this is the behavior most would expect.

This is turning something that would have "just worked" in about fifteen minutes into yet another quest for a messy workaround...

Thanks in advance,

Rick

_______________________________________________

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