Skip to content

Instantly share code, notes, and snippets.

@eventualbuddha
Created April 8, 2011 20:45
Show Gist options
  • Save eventualbuddha/910703 to your computer and use it in GitHub Desktop.
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.
@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