Path: utzoo!attcan!uunet!ginosko!gem.mps.ohio-state.edu!apple!sun-barr!decwrl!shelby!polya!ali From: ali@polya.Stanford.EDU (Ali T. Ozer) Newsgroups: comp.sys.next Subject: Re: Variable Length Argument Lists for Methods--REPOST Message-ID: <12004@polya.Stanford.EDU> Date: 28 Sep 89 02:47:49 GMT References: <1989Sep23.215508.5568@lighthouse.com> Sender: Ali T. OzerReply-To: aozer@NeXT.com (Ali Ozer) Organization: . Lines: 45 [I actually posted an answer from NeXT a few days ago, but I think the message never made it anywhere. So here it goes again; if you see this twice, apologies... -Ali] In article <1989Sep23.215508.5568@lighthouse.com> Brian Douglas Skinner writes: >We're trying to write a method that takes a variable number of >arguments, something like: > >[error notify: "Error at line %i in file %s", __LINE__, __FILE__]; Use of variable length argument lists is described in the Second Edition of Kernighan & Ritchie; you can use the same sort of mechanism to go through the arguments in ObjC. Say you have (very simple) method which takes a number and that many string arguments, and prints the strings one after another. The declaration would be: - listItems:(int)numItems, ...; ... the code itself would be: - listItems:(int)numItems, ... { va_list ap; va_start (ap, numItems); // Make ap point to the first unnamed argument printf ("The Items are: "); while (numItems--) printf ("%s ", va_arg(ap, char *)); // All args should be char * printf ("\n"); va_end (ap); // Wrap it up... } ... and a sample call could be: [self listItems:3, "foo", "bar", "zap"]; The above example is of course too simple; on page 156 of K&R there's a more complicated example that implements a minimal printf. If you just want to have an error: method that takes a format string and bunch of args and treats them like printf would, you can vfprintf (or vsprintf). The man page has the necessary info... Ali Ozer, NeXT Developer Support Ali_Ozer@NeXT.com