• 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 lists
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable argument lists


  • Subject: Re: Variable argument lists
  • From: matt neuburg <email@hidden>
  • Date: Sat, 22 May 2004 12:08:56 -0700

On Sat, 22 May 2004 03:22:20 -0400, Ken Tozier <email@hidden> said:
>I'm writing a class and would like to include an initialization method
>like this one from NSDictionary:
>
>+ (id) dictionaryWithObjectsAndKeys:(id) inFirst, ...
>
>But I'm not really sure how to go about it. Could someone show me what
>to do once I'm inside the initialization method?

Here's an actual quasi-useful example:

- (void) manyStrings: (NSString*) s, ... {
va_list ap;
va_start(ap, s);
NSString* p = s;
while (p) {
NSLog(@"%@", p);
p = va_arg(ap, NSString*);
}
va_end(ap);
NSLog(@"Done!");
}

- (IBAction)doButton:(id)sender
{
[self manyStrings: @"mannie", @"moe", @"jack", nil];
}

Notice that you need a way of detecting when you've reached the last arg;
there is no way to ask how big the argument list is (because it's variable
in length!). This is why things like arrayWithObjects: require that you
supply nil as the last arg, and that's why I've done the same in this
example. m.
--
matt neuburg, phd = email@hidden, http://www.tidbits.com/matt/
pantes anthropoi tou eidenai oregontai phusei
AppleScript: the Definitive Guide! NOW SHIPPING...! (Finally.)
http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
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.


  • Prev by Date: Re: Variable argument lists
  • Next by Date: XCode style toolbar buttons
  • Previous by thread: Re: Variable argument lists
  • Next by thread: Re: Variable argument lists
  • Index(es):
    • Date
    • Thread