Rightpoint / RZDataBinding

Lightweight KVO-based data binding options.
Other
543 stars 57 forks source link

callImmediately crashes if used with unresolvable key path #34

Closed ZevEisenberg closed 9 years ago

ZevEisenberg commented 9 years ago

I'm subclassing NSLayoutConstraint, and I want to know when the firstItem’s font changes. So I set it up like this:

- (void)configureObservers
{
    NSString *firstItemFontKeyPath = [@[
                                        NSStringFromSelector(@selector(firstItem)),
                                        NSStringFromSelector(@selector(font)),
                                        ] componentsJoinedByString:@"."];

    [self rz_addTarget:self
                action:@selector(updateConstant)
      forKeyPathChange:firstItemFontKeyPath
       callImmediately:YES];
}

Notice that I'm building the key path myself. This is because firstItem is of type id, and depending on what type of view it is, it may not have a font property.

If I run this code as-is, I get a crash:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DashedHairlineView 0x7fdaa492cbc0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key font.'

If I delete the callImmediately parameter and call the handler myself on the next line, it works fine, and subsequent changes successfully trigger the callback.

jvisenti commented 9 years ago

This will be fixed when #35 is implemented.

jvisenti commented 9 years ago

Fixed.