Variadic Objective-C Functions ( variable function parameters )
April 23, 2014
What are variadic functions?
Well, to be honest, I wasn’t exactly sure the name of these kinds of functions for years after I started programming. Just knowing the terminology will make your life a lot easier when googling for this same kind of thing in different programming languages.
In computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. (stolen from Wikipedia)
In the simplest of terms, it is a function that takes any number of arguments vs a function that takes a defined number of arguments.
iOS Library usage
There are multiple places that you run into variadic functions when doing iOS development. They are fairly simple to understand and use. The variable parameter can contain a comma seperated list of values and must be
Nil terminated. Example below demonstratest creating a
UIAlertView without any syntax
Implementing your own Variadic function
Using these functions is simpler than creating your own function that accepts a variable parameter. Much of the syntax and usage is inherited from C, so it may seem a little bit foreign if Objective-C is all you know. Example below demonstrates sorting any number of
The first thing to notice is the method definition. This is just syntax, but the
... signifies that this is a variable argument. The second thing is the
nil check, values will always be a pointer to the first element of the variable argument, if the first element is
nil we simply return an empty array.
The c-style stuff comes into play when you reach the
va_list line. It is pretty simple, but you create a
va_list and start reading from that list using
va_start. I used a
do , while loop because the
va_arg function will move the pointer, and I wanted access to the first item. After you are done, of course, call
That’s all there is to making variadic functions in iOS