5

From Objective C Programming Guide (Under the "Object Messaging" section),

Methods that take a variable number of parameters are also possible, though they’re somewhat rare. Extra parameters are separated by commas after the end of the method name. (Unlike colons, the commas are not considered part of the name.) In the following example, the imaginary makeGroup: method is passed one required parameter (group) and three parameters that are optional:

[receiver makeGroup:group, memberOne, memberTwo, memberThree];

I tried to create such a method and it shows an error

"Expected ';' after method prototype"

when I try to declare the below function in my interface file(.h file).

- (void) printMyClass: (int) x, (int) y, (int) z;

Can anyone give sample example to create such a method like makeGroup

Thank you

Community
  • 1
  • 1
Confused
  • 3,846
  • 7
  • 45
  • 72
  • do you only want a fixed N number of parameters or do you want it to be a variable N (i.e. x, y, z and possibly letters after that?)? – Michael Dautermann Sep 17 '12 at 07:04
  • 1
    See [Technical Q&A 1405, How can I write a method that takes a variable number of arguments, like `+\[NSString stringWithFormat:\]?](http://developer.apple.com/library/mac/#qa/qa1405/_index.html) – jscs Sep 17 '12 at 07:05
  • possible duplicate of [Variable length parameters in Objective-C](http://stackoverflow.com/questions/5458428/variable-length-parameters-in-objective-c) – jscs Sep 17 '12 at 07:07
  • Fixed N numbers of parameters.. It will be good if you also leave a note about how-to-do the variable N parameter(dynamic number of parameters) – Confused Sep 17 '12 at 07:07

2 Answers2

16

You can see this link.

In your header file define the methods with three dots at the end

-(void)yourMethods:(id)string1,...;

And in you implementation file write the methods body

-(void)yourMethods:(id)string1, ...{

    NSMutableArray *arguments=[[NSMutableArray alloc]initWithArray:nil];
    id eachObject;
    va_list argumentList;
    if (string1) 
    {
        [arguments addObject: string1];
        va_start(argumentList, string1); 
        while ((eachObject = va_arg(argumentList, id)))    
        {
             [arguments addObject: eachObject];
        }
        va_end(argumentList);        
     }
    NSLog(@"%@",arguments);
}

Now call your method

[self yourMethods:@"ab",@"cd",@"ef",@"gf",nil];

NOTE: remember to put nil at the end

Neo
  • 2,807
  • 1
  • 16
  • 18
  • Thanks for the answer. So, Should I use "NSMutableArray" to implement the multiple parameters in method? – Confused Sep 17 '12 at 09:23
  • Sorry i dint understand your question. Where do u want to use mutable array, while calling the method or inside the body of the method?...You can see the link above where the expalination has been done – Neo Sep 17 '12 at 10:11
  • Sorry.. I confused little bit. So the yourMethods method and methodWithVariableParameters method is same? – Confused Sep 20 '12 at 06:24
  • what if argument is not of kind id?? for example a BOOL: '[self yourMethods:@"ab", 1,nil];' help me here please.. – Frade Oct 25 '13 at 17:24
  • Thanks for the answer. But if I want to call this method in another method which also takes variable number of method parameters and need to pass these parameters to the called method, what should I do? – lxmfly123 Jul 26 '16 at 09:30
0

The syntax for declaring a method with a variable number of arguments is like this:

- (void) printMyClass: (int) x, ...;

One argument is always the required minimum, the others can be accessed via the va_arg function group. For the exact details, see this tutorial.

waldrumpus
  • 2,540
  • 18
  • 44