Skip to content

Instantly share code, notes, and snippets.

@priore
Last active August 29, 2015 14:01
Show Gist options
  • Save priore/8a1556fa23a5e02ed102 to your computer and use it in GitHub Desktop.
Save priore/8a1556fa23a5e02ed102 to your computer and use it in GitHub Desktop.
Fixes crash in case of missing of the partial KeyPath in NSDictionary
@implementation NSDictionary (Values)
- (id)valueForKeyPath:(NSString *)keyPath
{
__block id value = nil;
if (keyPath != nil) {
NSArray *keys = [keyPath componentsSeparatedByString:@"."];
if (keys && [keys count] > 0) {
[keys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
value = idx == 0 ? [self objectForKey:obj] : [value objectForKey:obj];
if (value == nil || ![value isKindOfClass:[NSDictionary class]])
*stop = YES;
}];
}
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment