views:

29

answers:

1

Does iPhone key-value-coding have a way to test whether a class will accept a given key?

That is, without implementing the valueForUndefinedKey: or setValue: forUndefinedKey: method of the target class, I want to be able to do something like this:

if ([targetObject knowsAboutKey: key])  {
  [targetObject setValue: value forKey: key];
}
+1  A: 

The default implementation of setValue:forUndefinedKey: raises an NSUndefinedKeyException. You could surround the attempt in a try/catch block:

@try{
    [targetObject setValue:value forKey:key];
} @catch (NSUndefinedKeyException *e) }
    NSLog(@"oh well... handle the case where it has no key here.");
}
kevboh