Cocoa bindings call valueForKey: instead of valueForKeyPath:

I have an NSPopUpButton that is attached to a subclass, with the following overridden methods:

- (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 links are as follows:

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

Instead of being called [Object valueForKeyPath:@"availableVoices.name"]

, valueForKey:

called for every key in a key. availableVoices

has no definition for name

, so it crashes.

Shouldn't you valueForKey:

be called after valueForKeyPath:

, if at all?

+3


source to share


1 answer


Your comment suggests a reason for what you are seeing. If the path "Content" is to be prefixed with "Content Values" then getting first first and then last relative to it makes sense. Also, although Cocoa Bindings uses KVO, that doesn't mean it should use valueForKeyPath

.



0


source







All Articles