SHARE
TWEET

Fixes crash in case of missing of the partial KeyPath

priore May 23rd, 2014 616 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
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top