Is the first argument of the Objective-C variative function required?

Here is an example of a variational function in Obj C.

// This method takes an object and a variable number of args - (void) appendObjects:(id) firstObject, ...; 

Is it really necessary that the first argument be an object of Obj C? If not, what should be the syntax?

EDIT: Thanks for your answers - the first argument should not be NSObject , but I wanted to ask: Is it possible to refuse the first argument at all? I probably did not correctly formulate the question for the first time; Sorry for this

 - (void) appendObjects: ...; 

The following error appears in the above declaration: Expected ';' after method prototype Expected ';' after method prototype

+6
source share
3 answers

It does not have to be true. There are two hidden arguments for each Objective-C method, self and _cmd (in that order). self is self-evident (ha ha), but less known is _cmd , which is just the selector that was used to call the current method. This allows you to use variable arguments with Objective-C methods, it would seem, without using an initial argument, as you do with the standard variational function C.

 - (void) someMethod:... { va_list va; va_start(va, _cmd); // process all args with va_arg va_end(va); } 

Then you can call the method as follows:

  [someObj someMethod:1, 2, 3]; 
+6
source

+3
source

No, it should not be an object. You can write a variational function using float, for example:

 - (void) doSomethingWithFloats: (float) float1, ...; 
+2
source

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


All Articles