Created
October 24, 2012 16:09
-
-
Save lbrndnr/3947009 to your computer and use it in GitHub Desktop.
Calling a method if the receiver does respond to it.
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
-(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:¶meter 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