Skip to content

Instantly share code, notes, and snippets.

@tarunon
Created February 11, 2014 13:25
Show Gist options
  • Save tarunon/8934732 to your computer and use it in GitHub Desktop.
Save tarunon/8934732 to your computer and use it in GitHub Desktop.
NSArray's valueForKey is very convenient! :)
#import <Foundation/Foundation.h>
@interface NSArray (objectForKeyedSubscript)
- (id)objectForKeyedSubscript:(id)key;
@end
@implementation NSArray (objectForKeyedSubscript)
- (id)objectForKeyedSubscript:(id)key
{
return [[self valueForKey:key] firstObject];
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *ary = @[@{@"AAA": @"BBB"}];
NSLog(@"%@", ary[@"AAA"]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment