Created
April 8, 2011 20:45
-
-
Save eventualbuddha/910703 to your computer and use it in GitHub Desktop.
Allows you to selectively ignore undefined keys when setting a value by key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation CPObject (CPCodingExtensions) | |
- (void)setValue:(id)aValue forKey:(CPString)aKey ignoreUndefinedKey:(BOOL)ignoreUndefinedKey | |
{ | |
if (!ignoreUndefinedKey) | |
{ | |
[self setValue:aValue forKey:aKey]; | |
} | |
else | |
{ | |
try { [self setValue:aValue forKey:aKey]; } | |
catch (e) | |
{ | |
if (!e.isa || ![e isKindOfClass:[CPException class]] || // a non-ObjJ error object | |
[e name] != CPUndefinedKeyException || | |
[[e userInfo] objectForKey:CPTargetObjectUserInfoKey] != self || | |
[[e userInfo] objectForKey:CPUnknownUserInfoKey] != aKey) | |
// this exception wasn't triggered because we don't have the property, | |
// but for some other reason, so we should pass it along | |
throw e; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment