Detect selector signature

How can I check the signature of the selector or does the selector need parameters or not?

eg. I want to check if the selector is of type
-(void) method
or
-(void) method:(id)param

+5
source share
2 answers

You can get a lot of information about a particular selector with a class NSMethodSignature:

id obj = ...
SEL selector = ...

NSMethodSignature *signature = [obj methodSignatureForSelector:selector];
NSUInteger args = [signature numberOfArguments];
int i;
for(i = 0; i < args; i++)
   printf("argument type at index %d: %c", i, [signature getArgumentTypeAtIndex:i]);
+8
source

You can check this with respondsToSelector:, i.e. something like this:

if ( [myObject respondsToSelector:@selector(doSomethingWithOneArgument:)] ){
    ....
}
0
source

All Articles