views:

122

answers:

2

I've got an NSArrayController, and I'm using KVO to observe the Old/New values of it's selection method.

This works perfectly (triggers when the selection changes, the usual) except that the items in the change dictionary are all null instead of being the old/new selected object. [arrayController selection] still returns the proper object, but I'd like to be able to access the previously selected object as well if possible (my workaround will probably be to observe the selected index instead and see if that works).

The only possible reason for this I've come up with is perhaps it's because the NSArrayController is a proxy object.

So is this the expected behavior, or is something weird going on?

EDIT: I tried observing just the Indexes, but that didn't work either. Both old and new keys still show up as null.

A: 

For plain KVO (as opposed to bindings), try observing the selectedObjects property instead of the selection property. That will give you a straightforward array of objects instead of the proxy objects which are used by the Cocoa bindings system. I believe the old/new keys should accurately reflect the change in the selection that way.

Brian Webster
Unfortunately, that doesn't work! I've tried every single method I could find, from selection to selectedObjects to selectedIndex to selectedIndexes, etc. They all return a dictionary consisting of one NSKeyValueChangeKindKey with a value of 1, and the other two keys both have values of NSNull.
Lawrence Johnston
A: 

Are you using NSKeyValueObservingOptionNew, and NSKeyValueObservingOptionOld in the options of addObserver:forKeyPath:options:context:?

If you could show us some code it would be helpful.

Tom Dalling