Last active
August 29, 2015 14:01
-
-
Save priore/8a1556fa23a5e02ed102 to your computer and use it in GitHub Desktop.
Fixes crash in case of missing of the partial KeyPath in NSDictionary
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 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