Canceling modifications in a NSMutableDictionary

I have the following problem:

I want to edit an NSMutableDictionary into some fields of a window. The window has two buttons, “OK” to confirm changes, and “Cancel” to revert to the previous state.

How can I implement this? I thought to make a copy from the NSMutDict before editing it, and restore this copy if the user presses Cancel.

But I get errors like these:

  • this class is not key value coding-compliant for the key xy
  • Cannot remove an observer <NSArrayController 0x2006f0d80> for the key path “xy” from <NSCFDictionary 0x2004f1de0>, most likely because the value for the key “xy” has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the NSCFDictionary class.

What am I doing wrong? I suspect that I can’t do these changes behind the scenes.

Notes:

  1. the NSMutDict is in a NSMutableArray of NSMutDicts
  2. the window fields are bound to the different keys of the NSMutDict.

It’s hard to know what you are doing wrong since you didn’t show what you did to generate those errors. Without knowing the flow of your program, I can’t be sure of what might be a better solution – you might be able to create a temporary dictionary to hold the old values, and then if the user clicks cancel, you restore your dictionary with setValue_forKey_ for each of the changed values.

Ric

I tried this:

log oRefRecord of (first item of gObjectController's selectedObjects())

gives

and then :

        set gObjectCopy to current application's NSDictionary's initWithDictionary_copyItems_(oRefRecord of (first item of gObjectController's selectedObjects()),true)

but got:

I logged first to check the oRefRecord : it has the correct NSDictionary structure. Then I wanted to do a “deep copy” of it (not only the container but the values).

So what’s wrong this time ? :confused:

You left out an alloc()'s in there.

Hello Shane! Thank you, I’m now one error further:
The reverse operation (if Cancel clicked) :

oRefRecord of first item of gObjectController's selectedObjects()'s setDictionary_(gEditedObject)

gives this error:

How could this oRefRecord become a NSCFArray, from the NSCFDictionary it was a couple of seconds before? And what are these CF attributes?

That’s not what the error is saying. It’s telling you that the object you are trying to call setDictionary: on is an NSCFArray, and as such doesn’t understand that method. Look at your code and you see that you are calling setDictionary: on gObjectController’s selectedObjects()'s, which presumably returns an array.

What CF attributes?

I thought that you were calling setDictionary on the whole first part of that statement with that syntax, which should be a dictionary, but maybe Shane is right, that the setDictionary is being sent to gObjectController’s selectedObjects()'s. Maybe try putting in parentheses and see if that makes any difference:

(oRefRecord of first item of gObjectController’s selectedObjects())'s setDictionary_(gEditedObject)

Many of the NSObjects are bridged from the core foundation objects, hence the CF. Don’t worry about that, an NSCFArray is what you often get when you log an NSArray.

Ric

It does. Well, I feel a child like facing a problem of algebra. Everything was a parenthesis level away!

No excuses. Thank you, you two!