• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Variable argument methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable argument methods


  • Subject: Re: Variable argument methods
  • From: Prachi Gauriar <email@hidden>
  • Date: Thu, 10 Jul 2003 20:24:13 -0500

On Thursday, July 10, 2003, at 6:24 PM, Matt Diephouse wrote:

I'm trying to write a method that takes a variable number of arguments. I've searched the archives and the net for examples, but haven't understood anything I've come up with. I want to take the following method (which is untested) and make so I'm not using an array to pass arguments.

- (BOOL)sendCommand: (NSString *)cmd
withArgs: (NSArray *)args;

Here's an example of how I want its usage to look.

[class sendCommand:@"article"
withArgs:@"one", @"two", @"three"];


Typically what's done is you actually make that list nil terminated, so your actual usage would be

[class sendCommand:@"article" withArgs:@"one", @"two", @"three", nil];

Here's how you do it.

Your declaration:

- (BOOL)sendCommand:(NSString *)cmd withArgs:(id)arg1,...;

Your definition:

#include <stdarg.h>

- (BOOL)sendCommand:(NSString *)cmd withArgs:(id)arg1,...
{
// This is required to collect the items
va_list argList;

// Just used here as an example for collecting the args for your usage
NSMutableArray *argumentArray = [NSMutableArray arrayWithObject:arg1];
id argument;

// When you start collecting the arguments, call va_start()
// Here your args are the va_list created earlier and the
// argument before the '...' in your method declaration
va_start(argList, arg1);

// va_arg retrieves the next argument. Its arguments are the va_list you created
// earlier and the type of the argument you're retrieving
while (argument = va_arg(argList, id)) {
[argumentArray addObject:argument];
}

// Do this at the end, once you're done getting arguments
va_end(argList);

// Insert your code here
}

More info and examples can be found in the stdarg manpage.

HTH.
-Prachi
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Variable argument methods (From: Matt Diephouse <email@hidden>)

  • Prev by Date: Re: moving breakpoints
  • Next by Date: splitting lines
  • Previous by thread: Re: Variable argument methods
  • Next by thread: moving breakpoints
  • Index(es):
    • Date
    • Thread