Re: Variadic arguments to methods in Objective-C - How ?
Re: Variadic arguments to methods in Objective-C - How ?
- Subject: Re: Variadic arguments to methods in Objective-C - How ?
- From: Gwynne <email@hidden>
- Date: Sat, 8 Jan 2005 15:18:52 -0500
On Jan 8, 2005, at 3:04 PM, Tommy Nordgren wrote:
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.
Untrue. For example, the [NSString stringWithFormat:, ...] method. The
syntax is:
// In your @interface
- (void)variadicMethod:(id)firstArg, ...;
// Method call
[object variadicMethod:format, arg1, arg2, /* etc. */];
// In your @implmentation
- (void)variadicMethod:(id)firstArg, ...
{
va_list args;
va_start( args, firstArg );
// Use the usual va_arg() syntax here
va_end( args );
}
The method must take at least one non-variadic argument, as far as I
know. All the usual rules about variadic arguments apply. It follows
the exact semantics of stdarg(3), and you can pass Objective-C objects
as variadic arguments.
-- Gwynne, key to the Code
Email: email@hidden
Website: http://musicimage.plasticchicken.com/
"This whole world is an asylum for the incurable."
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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