How to get the arity of the method?

In Javascript, for example, you can get arity (the number of arguments the function should take) just func.length . How can I get this information for a method in Objective-C?

+1
source share
3 answers

NSMethodSignature is terrific for all kinds of reflection information.

 SEL selector = @selector(foo:); NSMethodSignature *sig = [someObj methodSignatureForSelector:selector] int argCount = [sig numberOfArguments]; 
+7
source

If the receiving object does not send -methodSignatureForSelector: to, you can use it NSStringFromSelector() and count the number of occurrences of the colon character.

+2
source

From Objective-C Link to the working environment

method_getNumberOfArguments

Returns the number of arguments accepted by the Method.

unsigned method_getNumberOfArguments (method method) Parameter method Pointer to the data structure of the method. Go through the method in question. The return value is an integer containing the number of arguments accepted by this method.

+1
source

Source: https://habr.com/ru/post/1336677/


All Articles