• 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: Variadic arguments to methods in Objective-C - How ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variadic arguments to methods in Objective-C - How ?


  • Subject: Re: Variadic arguments to methods in Objective-C - How ?
  • From: Evan Schoenberg <email@hidden>
  • Date: Sat, 8 Jan 2005 14:19:55 -0600

Vincent,

Below is an example method which takes a variable number of arguments; it's a glorified printf, really. Hope it helps.

- (void)adiumDebug:(NSString *)message, ...
{
va_list ap; /* Points to each unamed argument in turn */
NSString *actualMessage;


va_start(ap, message); /* Make ap point to the first unnamed argument */
actualMessage = [[NSString alloc] initWithFormat:message
arguments:ap];



/* Do something with actual message */

/* clean up when done */
[actualMessage release];
va_end(ap);
}

-Evan
www.adiumx.com

On Jan 8, 2005, at 2:13 PM, Vincent Coetzee wrote:

I am sorry to disagree, but I must, Objective-C must allow variadic arguments, because many of the Foundation classes uses them such as NSArray, for example

[NSArray arrayWithObjects: obj1,obj2,etc,nil]

also

[NSString stringWithFormat: string,arg1,...argn]

Vincent

On Jan 08, 2005, at 22:04, Tommy Nordgren wrote:



Jan 8, 2005 kl. 8:38 PM skrev Vincent Coetzee:


Dear List,

How does one create methods in Objective-C that use a variable number of arguments. I am familiar with the normal C mechanism, but does this work in Objective-C considering the hidden arguments to methods (i.e. _self and _cmd) ? So can I use the normal C mechanism and if not how is it done ? Some sample code would be appreciated.



As far as I know, the objective C syntax don't allow variadic methods. However,
by giving the source files the suffix .mm, you can use C++ as well to a certain extent, although it is tricky, and according to the documentation not possible to embed c++ objects with virtual methods directly in Objective C classes. You can hovever use pointers to refer to objective c classes from c++ code and vice versa.
Also, it IS possible to embed most C++ classes in Objective C, althought it can be tricky because there is no built in language support for this.
Hope this helps.





_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Variadic arguments to methods in Objective-C - How ?
      • From: Vincent Coetzee <email@hidden>
References: 
 >Variadic arguments to methods in Objective-C - How ? (From: Vincent Coetzee <email@hidden>)
 >Re: Variadic arguments to methods in Objective-C - How ? (From: Tommy Nordgren <email@hidden>)
 >Re: Variadic arguments to methods in Objective-C - How ? (From: Vincent Coetzee <email@hidden>)

  • Prev by Date: Re: Variadic arguments to methods in Objective-C - How ?
  • Next by Date: Re: Variadic arguments to methods in Objective-C - How ?
  • Previous by thread: Re: Variadic arguments to methods in Objective-C - How ?
  • Next by thread: Re: Variadic arguments to methods in Objective-C - How ?
  • Index(es):
    • Date
    • Thread