Here is an example of a variadic function in Obj C.
// This method takes an object and a variable number of args
- (void) appendObjects:(id) firstObject, ...;
Is it really mandatory to have the first argument as an Obj C object? If not, what should be the syntax?
EDIT: Thanks for your responses – the first argument does not need to be an NSObject, but what I meant to ask is: Is it possible to do away with the first argument altogether? I probably did not frame the question well the first time around; sorry about that
- (void) appendObjects: ...;
The above declaration throws the following error: Expected ';' after method prototype
It doesn’t have to be anything really. There are two hidden arguments to every Objective-C method,
self, and_cmd(in that order).selfis self-explanatory (haha), but a lesser-known one is_cmd, which is simply the selector that was used to invoke the current method. This makes it possible to use variadic arguments with Objective-C methods seemingly without using an initial argument like you do with a standard variadic C function.Then you can call the method like this: