Skip to content

Instantly share code, notes, and snippets.

@rugginoso
Created August 17, 2012 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rugginoso/3383407 to your computer and use it in GitHub Desktop.
Save rugginoso/3383407 to your computer and use it in GitHub Desktop.
Dynamically call delegate method by name in Objective-C
- (void)dispathCommand:(NSString *)command withParameters:(NSArray *)parameters
{
SEL *selector = NSSelectorFromString([NSString stringWithFormat:[@"handle" stringByAppendingString:[command capitalizedString]]);
if (![self.delegate repondsToSelector:selector]) {
[self handleUnknownCommand:command withParameters:parameters];
return;
}
NSMethodSignature *signature = [[self.delegate class] instanceMethodSignatureForSelector:selector];
if (signature != nil) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self.delegate];
[invocation setSelector:selector];
[invocation setArgument:&parameters atIndex:2];
[invocation invoke];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment