Skip to content

Instantly share code, notes, and snippets.

@priore
Created October 25, 2013 23:39
Show Gist options
  • Save priore/7163483 to your computer and use it in GitHub Desktop.
Save priore/7163483 to your computer and use it in GitHub Desktop.
How to invoke a method in a class via their string names
+ (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