Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable number of method parameters in Objective C - Need an example

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

like image 970
Confused Avatar asked Sep 17 '12 06:09

Confused


People also ask

Can the number of parameters be varied in a method?

The number of parameters can be varied; parameters – a name that corresponds to an array of parameters. 2. What are the advantages of using variable number of arguments in methods? Using a variable number of arguments gives the following advantages: you can pass a different number of arguments to the same method.

How to declare a method with a variable number of arguments?

The general form of a method declaration with a variable number of arguments is: return_type MethodName ( params type [] parameters ) { // ... } type – the type of parameters that the method receives. The number of parameters can be varied; parameters – a name that corresponds to an array of parameters. 2.

What is a variable in Objective C?

Objective-C Variables. A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in Objective-C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

What is variable number of arguments in C++?

Variable number of arguments in C++. Sometimes, you may come across a situation, when you want to have a function, which can take variable number of arguments, i.e., parameters, instead of predefined number of parameters.


1 Answers

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

like image 180
Neo Avatar answered Sep 20 '22 15:09

Neo