Cocoa bindings call valueForKey: instead of valueForKeyPath:

Viewed 86

I have an NSPopUpButton which is bound to a subclassed object, with the following methods overwritten:

- (id)valueForKeyPath:(NSString *)keyPath {
    NSLog(@"valueForKeyPath: %@", keyPath);
    if ([keyPath hasSuffix:@"availableVoices.name"]) {
        return self.availableVoiceNames;
    } else {
        return [super valueForKeyPath:keyPath];
    }
}

- (id)valueForKey:(NSString *)key {
    NSLog(@"valueForKey: %@", key);
    return [super valueForKey:key];
}

The bindings are as follows:

  • Content -> Object.availableVoices
  • Content Values -> Object.availableVoices.name

Instead of [Object valueForKeyPath:@"availableVoices.name"] being called, valueForKey: is called on each key in the key path. availableVoices doesn't have a definition for name so it crashes.

Isn't valueForKey: supposed to be called after valueForKeyPath:, if at all?

1 Answers
Related