Search code examples
objective-cvariadicterminator

ObjC variadic arguments: hint terminator


In Xcode, if I start typing "[NSArray arrayWith" I get the following hint:

id arrayWithObjects:(id), ..., nil

As you can see, the terminator is explicit.

If I write a method taking variadic arguments by myself, I get this hint

id myMethod:(id), ...

(the terminator is not shown). How can I hint the terminator I want to use?


Solution

  • Apply the

    __attribute__((sentinel))
    

    attribute to your variadic method, or alternatively, use the

    NS_REQUIRES_NIL_TERMINATION
    

    macro, which expands to the same. This way, the compiler will know that your variadic argument list needs to be 0-terminated.

    - (void)foo:(id)arg1, ... NS_REQUIRES_NIL_TERMINATION
    {
        va_list args;
        va_start(args, arg1);
        // ...
    }