Skip to content

Instantly share code, notes, and snippets.

@terhechte
Created January 15, 2013 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terhechte/4541442 to your computer and use it in GitHub Desktop.
Save terhechte/4541442 to your computer and use it in GitHub Desktop.
Example of how to use the category key/value approach from my blogpost here: http://appventure.me/2011/12/fast-nsdictionary-traversal-in-objective-c.html
#import <Cocoa/Cocoa.h>
@interface NSDictionary (objectForKeyList)
- (id)objectForKeyList:(id)key, ...;
@end
@implementation NSDictionary (objectForKeyList)
- (id)objectForKeyList:(id)key, ...
{
id object = self;
va_list ap;
va_start(ap, key);
for ( ; key; key = va_arg(ap, id))
object = [object objectForKey:key];
va_end(ap);
return object;
}
@end
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary *aDictionary = [NSDictionary dictionaryWithObject:
[NSDictionary dictionaryWithObject:
[NSDictionary dictionaryWithObject:
[NSDictionary dictionaryWithObject: @"Magnus Enzensberger" forKey:@"friend"]
forKey: @"data"]
forKey: @"likes"]
forKey: @"data"];
NSLog(@"%@", [aDictionary objectForKeyList: @"data", @"likes", @"data", @"friend", nil]);
[pool release];
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment