Skip to content

Instantly share code, notes, and snippets.

@lbrndnr
Created October 24, 2012 16:09
Show Gist options
  • Save lbrndnr/3947009 to your computer and use it in GitHub Desktop.
Save lbrndnr/3947009 to your computer and use it in GitHub Desktop.
Calling a method if the receiver does respond to it.
-(BOOL)performSelectorIfResponding:(SEL)selector withObjects:(id)firstParameter, ... {
if (![self respondsToSelector:selector]) {
return NO;
}
NSMethodSignature* signature = [self.class instanceMethodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
invocation.selector = selector;
NSUInteger i = 2;
va_list parameters;
va_start(parameters, firstParameter);
for (id parameter = firstParameter; parameter; parameter = (va_arg(parameters, id))) {
[invocation setArgument:&parameter atIndex:i];
i++;
}
va_end(parameters);
[invocation invoke];
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment