Skip to content

Instantly share code, notes, and snippets.

@tylerhall
Created August 19, 2010 04:04
Show Gist options
  • Save tylerhall/536994 to your computer and use it in GitHub Desktop.
Save tylerhall/536994 to your computer and use it in GitHub Desktop.
NSDictionary category that lets you retrieve a value from a dictionary of dictionaries in a single statement. I'm gonna feel really dumb if there was already a way to do this.
#import <Foundation/Foundation.h>
@interface NSDictionary (EmbeddedValue)
- (id)embeddedValueWithKeys:(NSArray *)keys;
@end
#import "NSDictionary+Embedded.h"
@implementation NSDictionary (EmbeddedValue)
- (id)embeddedValueWithKeys:(NSArray *)keys {
id obj = self;
for(id key in keys) {
obj = [obj objectForKey:key];
if(obj == nil)
return nil;
}
return obj;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment