Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable length parameters in Objective-C

How can i make a class method with variable length parameters, in Objective-C?

For example, a method like -arrayWithObjects:

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
like image 777
Mustafa Avatar asked Dec 06 '25 04:12

Mustafa


2 Answers

Take a look at varargs, e.g.: Apple Technical Q&A QA1405. It shouldn't matter whether the method is a class method or not.

like image 158
goetz Avatar answered Dec 08 '25 16:12

goetz


What you need is a variadic function. These functions take a flexible number of arguments, like NSLog, [NSArray arrayWithObjects:...], etc.

See this tutorial:

http://www.numbergrinder.com/node/35

Copied from my answer here: Obj-C, trying to write an alternative to NSLog, but I want my function to concatenate like NSLog?

like image 30
Evan Mulawski Avatar answered Dec 08 '25 17:12

Evan Mulawski