Created
October 25, 2013 23:39
-
-
Save priore/7163483 to your computer and use it in GitHub Desktop.
How to invoke a method in a class via their string names
This file contains hidden or 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
| + (void)invokeMethodNamed:(NSString*)methodName withObject:(id)object classNamed:(NSString*)className | |
| { | |
| id class = NSClassFromString(className); | |
| if (class) { | |
| SEL selector = NSSelectorFromString(methodName); | |
| if ([class respondsToSelector:selector]) { | |
| // static method +(void) | |
| [class performSelector:selector withObject:object]; | |
| } else { | |
| // new instanced method -(void) | |
| id object = [[[class alloc] init] autorelease]; | |
| if ([object respondsToSelector:selector]) { | |
| [object performSelector:selector withObject:object]; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment