priore

Fixes crash in case of missing of the partial KeyPath

May 23rd, 2014
709
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @implementation NSDictionary (Values)
  2.  
  3. - (id)valueForKeyPath:(NSString *)keyPath
  4. {
  5.     __block id value = nil;
  6.    
  7.     if (keyPath != nil) {
  8.         NSArray *keys = [keyPath componentsSeparatedByString:@"."];
  9.         if (keys && [keys count] > 0) {
  10.             [keys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  11.                 value = idx == 0 ? [self objectForKey:obj] : [value objectForKey:obj];
  12.                 if (value == nil || ![value isKindOfClass:[NSDictionary class]])
  13.             *stop = YES;
  14.             }];
  15.         }
  16.     }
  17.    
  18.     return value;
  19. }
RAW Paste Data