tags:

views:

52

answers:

2

How can I check the signature of the selector, or whether the selector requires parameters or not?

E.g. I want to check whether the selector is of type
-(void) method
or
-(void) method:(id)param

A: 

You can check this using respondsToSelector:, ie something like this:

if ( [myObject respondsToSelector:@selector(doSomethingWithOneArgument:)] ){
    ....
}
JeffW
+6  A: 

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

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

NSMethodSignature *signature = [[obj class] instanceMethodSignatureForSelector:selector];

NSUInteger args = [signature numberOfArguments];
int i;
for(i = 0; i < args; i++)
   printf("argument type at index %d: %c", i, [signature getArgumentTypeAtIndex:i]);
Jacob Relkin