2

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];
Kirby Todd
  • 11,070
  • 3
  • 31
  • 58
Mustafa
  • 20,298
  • 41
  • 144
  • 209

3 Answers3

5

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.

goetz
  • 916
  • 6
  • 14
3

Methods that take variable arguments are known as variadic methods. The "..." is the variable argument.
For example, your function declaration would be: - (void)specialWithX:(NSInteger)x y:(NSInteger)y, ...;

For additional information take a look at Variable argument lists in Cocoa

Black Frog
  • 11,418
  • 1
  • 33
  • 66
3

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?

Community
  • 1
  • 1
Evan Mulawski
  • 53,455
  • 14
  • 114
  • 144