Malcolm:
I've created my own subclass of NSController and called exposeBinding for the various bindings available on it. Each binding corresponds exactly with a KVC-compliant pair of methods e.g. array and setArray. Now I can easily bind arrays of simple values to the binding. However, supposing I have an array of dictionaries, each dictionary with a key of "name", I find I cannot bind to array.name... it fails at runtime with a [<NSCFArray 0x3b85e0> addObserver:forKeyPath:options:context:] is not supported. Key path: name
is in setting up the observer (before you even get to the stage of receiving KVO notifications). You can't simply observe the array keypath, you need to register to observe the objects in the array (see for example, addObserver:toObjectsAtIndexes:forKeyPath:options:context:).
To clarify, I don't call addObserver:forKeyPath:options:context: myself, rather the NSController/NSObject supplied bind:toObject:withKeyPath: calls it on my behalf.
I managed to avoid this by overriding it like so:
- (void) bind: (NSString*) binding toObject: (id) observableController withKeyPath: (NSString*) keyPath options: (NSDictionary*) options { if ([binding isEqualToString: @"array"]) { // if I call super here instead, it blows up with the above error... [self setKeys: [observableController valueForKeyPath: keyPath]]; // just get the array value once... } else [super bind: binding toObject: observableController withKeyPath: keyPath options: options]; }
i.e. preventing the inbuilt bind:ToObject:withKeyPath: from calling through to addObserver:forKeyPath:, although I'm not fully comfortable with this approach since I'm not sure what else bind:ToObject:withKeyPath: is doing under the covers (yes, Malcolm, I've read your warning about calling super in bind:toObject:withKeyPath: on your very helpful web pages.)
Perhaps that is what NSPopUpButton is doing with its array-valued bindings? Is it calling addObserver:toObjectsAtIndexes:forKeyPath: instead in its bind:toObject:withKeyPath: implementation?
Cheers, Glen Low
--- pixelglow software | simply brilliant stuff www.pixelglow.com aim: pixglen
|