Objective-C Tips & Trick - Prioregroup.com
[Objective-C] Fixes crash in case of missing of the partial KeyPath
       
back

@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;
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com