Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c : Accessing variadic arguments in method [duplicate]

Possible Duplicate:
How to create variable argument methods in Objective-C
Variable number of method parameters in Objective C - Need an example

Following is an example of a method having variadic arguments.

- (void)numberOfParameters:group,... {
    NSLog(@"%@",group);
}

In above method, I know to access the first one of the variadic arguments. Would you please help me for accessing the others as well?

I am just going through ObjC.pdf & I am reading page number 35 & line number is 4

like image 213
Sagar Kothari Avatar asked May 19 '26 04:05

Sagar Kothari


1 Answers

See this almost same question

-(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);
}

Call it with nil parameter at the end as:

[object yourMethods:arg1,arg2,arg3,nil];// object can be self
like image 72
Neo Avatar answered May 20 '26 16:05

Neo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!