Created
June 23, 2013 08:42
-
-
Save danhd123/5844319 to your computer and use it in GitHub Desktop.
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
+ (id)objectForKeyedSubscript:(id)key | |
{ | |
unsigned int outCount; | |
if ([self instancesRespondToSelector:@selector(initWithCoder:)] && [key isKindOfClass:[NSCoder class]]) | |
{ | |
return [[self alloc] initWithCoder:key]; | |
} | |
//todo: cache these results in an associated object | |
Method *theseMethods = class_copyMethodList(self, &outCount); | |
NSMutableIndexSet *methodArrayHits = [[NSMutableIndexSet alloc] init]; | |
for (unsigned int i = 0; i < outCount; i++) { | |
NSString *initName = NSStringFromSelector(method_getName(theseMethods[i])); | |
if ([initName isEqualToString:@"initWithCoder:"]) | |
{ | |
continue; | |
} | |
if ([[NSString reasonableTypeEncodingForMethod:theseMethods[i]] isEqualToString:@"@@:@"] && [initName hasPrefix:@"initWith"]) | |
{ | |
[methodArrayHits addIndex:i]; | |
} | |
} | |
if ([methodArrayHits count] == 1) | |
{ | |
SEL sele = method_getName(theseMethods[[methodArrayHits firstIndex]]); | |
free(theseMethods); | |
return objc_msgSend(objc_msgSend([self alloc], sele, key), NSSelectorFromString(@"autorelease")); | |
} | |
return @"Foo!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about: