Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Declare the method like this in your <code>@interface</code>:</p> <pre><code>- (id)myObjectWithObjects:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION; </code></pre> <p>Then in the <code>@implementation</code> you would define it like this:</p> <pre><code>- (id)myObjectWithObjects:(id)firstObject, ... { va_list args; va_start(args, firstObject); for (id arg = firstObject; arg != nil; arg = va_arg(args, id)) { // Do something with the args here } va_end(args); // Do more stuff here... } </code></pre> <p>The <code>va_list</code>, <code>va_start</code>, <code>va_arg</code> and <code>va_end</code> are all standard C syntax for handling variable arguments. To describe them simply:</p> <ul> <li><code>va_list</code> - A pointer to a list of variable arguments.</li> <li><code>va_start</code> - Initializes a va_list to point to the first argument after the argument specified.</li> <li><code>va_arg</code> - Fetches the next argument out of the list. You must specify the type of the argument (so that va_arg knows how many bytes to extract).</li> <li><code>va_end</code> - Releases any memory held by the va_list data structure.</li> </ul> <p>Check out this article for a better explanation - <a href="http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html" rel="noreferrer">Variable argument lists in Cocoa</a></p> <hr> <p>See also: <a href="http://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html" rel="noreferrer">"IEEE Std 1003.1 stdarg.h"</a></p> <hr> <p>Another example from the <a href="http://developer.apple.com/library/mac/#qa/qa1405/_index.html" rel="noreferrer">Apple Technical Q&amp;A QA1405 - Variable arguments in Objective-C methods</a>:</p> <pre><code>@interface NSMutableArray (variadicMethodExample) - (void) appendObjects:(id) firstObject, ...; // This method takes a nil-terminated list of objects. @end @implementation NSMutableArray (variadicMethodExample) - (void) appendObjects:(id) firstObject, ... { id eachObject; va_list argumentList; if (firstObject) // The first argument isn't part of the varargs list, { // so we'll handle it separately. [self addObject: firstObject]; va_start(argumentList, firstObject); // Start scanning for arguments after firstObject. while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id" [self addObject: eachObject]; // that isn't nil, add it to self's contents. va_end(argumentList); } } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload