Variadic function: Difference between revisions
Content deleted Content added
No edit summary |
added objective c |
||
Line 257: | Line 257: | ||
(varargs "Mary "had "a "little "lamb) |
(varargs "Mary "had "a "little "lamb) |
||
apply "varargs [Mary had a little lamb] |
apply "varargs [Mary had a little lamb] |
||
=={{header|Objective-C}}== |
|||
Objective-C uses the same varargs functionality as C. Like C, it has no way of knowing the number or types of the arguments. Often, varargs parameters will be of type <code>id</code> (object); and the convention is that, if the number of arguments is undetermined, then the list must be "terminated" with <code>nil</code>. Functions that follow this convention include the constructors of data structures that take an undetermined number of elements, like <code>[NSArray arrayWithObjects:...]</code>. |
|||
<lang objc>#include <stdarg.h> |
|||
void printAll(id firstObject, ...) |
|||
{ |
|||
va_list args; |
|||
va_start(args, firstObject); |
|||
id obj; |
|||
for (obj = firstObject; obj != nil; obj = va_arg(args, id)) |
|||
NSLog(@"%@", obj); |
|||
va_end(args); |
|||
} |
|||
// This function can be called with any number or type of objects, as long as you terminate it with <code>nil</code>: |
|||
printAll(@"Rosetta", @"Code", @"Is", @"Awseome!", nil); |
|||
printAll([NSNumber numberWithInt:4], |
|||
[NSNumber numberWithInt:3], |
|||
@"foo", nil);</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |