Search code examples
objective-cruntimeobjective-c-runtimevariadic-functionsmethod-signature

Determining if an Objective-C method is variadic during runtime


Is there a way to find out -- at runtime -- whether a given method is of variadic type? Something like method_getTypeEncoding(); that won't tell me whether a method accepts variable number of arguments. Or is there maybe a trick to tell so?


Solution

  • Robert's comment is correct. Consider:

    @interface Boogity
    @end
    @implementation Boogity
    - (void)methodWithOneIntArg:(int)a {;}
    - (void)variadicMethodWithIDSentinel:(id)a, ... {;}
    @end
    

    Running strings on the resulting binary produces (there was also the stock main()):

    strings asdfasdfasdf 
    Boogity
    methodWithOneIntArg:
    variadicMethodWithIDSentinel:
    v20@0:8i16
    v24@0:8@16
    Hello, World!
    

    If I change the variadic method to be declared as - (void)variadicMethodWithIDSentinel:(int)a, ..., the strings output becomes:

    Boogity
    methodWithOneIntArg:
    variadicMethodWithIDSentinel:
    v20@0:8i16
    Hello, World!
    

    So, no, no way to tell.