Created
January 15, 2013 19:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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