IPhone-key-value encoding - check for key availability

Is there a way to encode iPhone keywords to check if the class will accept the given key?

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

if ([targetObject knowsAboutKey: key]) { [targetObject setValue: value forKey: key]; } 
+7
iphone key-value-coding
source share
1 answer

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

 @try{ [targetObject setValue:value forKey:key]; } @catch (NSException *e) { if ([[e name] isEqualToString:NSUndefinedKeyException]) { NSLog(@"oh well... handle the case where it has no key here."); // handle } else { [[NSException exceptionWithName:[e name] reason:[e reason] userInfo:[e userInfo]] raise]; } } 
+3
source share

All Articles