The scenario presents itself where I have an object that stores an outside @selector for later use. By design, I would like to be able to add two kinds of selectors. The simple one, without parameters, like [object add:@selector(doSomething)], and the more complex one, with one parameter, like [object add:@selector(doSomething:)] (mind the colon). Let’s say the selector is stored in a variable SEL mySelector.
In the execution, I need to decide between [anotherObject performSelector:mySelector] or [anotherObject performSelector:mySelector withObject:userInfo]].
The way I implemented this decision, is by providing a BOOL flag that redundantly stores whether the performance should be with or without the extra parameter. Yet although I can’t find this in the docs, I have the feeling that I should also be able to ask the selector something like -(BOOL)needsParameter. I know, for example, that UIGestureRecognizer’s addTarget:action: somehow makes this distinction automatically.
Could someone point me in the right direction?
You can use the
NSMethodSignatureclass for that. For instance,Alternatively, you could use
NSStringFromSelector()to get the string representation ofmySelectorand count the number of occurrences of the colon character.