Re: indeterminate number of arguments
Re: indeterminate number of arguments
- Subject: Re: indeterminate number of arguments
- From: Sherm Pendley <email@hidden>
- Date: Wed, 16 Feb 2011 18:29:03 -0500
On Wed, Feb 16, 2011 at 6:26 PM, Sherm Pendley <email@hidden> wrote:
> On Wed, Feb 16, 2011 at 6:12 PM, Todd Heberlein <email@hidden> wrote:
>> I see methods like this one (for NSArray) with an indeterminate number of arguments
>>
>> - (id)initWithObjects:(id)firstObj, ...
>>
>> How can I declare my own method like this
>
> Just like above, with ..., as in:
>
> - (id) initWithObjects:(id)firstObject, ...;
>
>> and how, in the implementation, do I access the set of arguments passed in?
>
> Using the va_* macros found in stdarg.h. For example:
>
> - (id) initWithObjects:(id)firstObject, ... {
> va_list args;
> id nextObject;
> NSMutableArray *arr = [NSMutableArray arrayWithObject:firstObject];
>
> va_start(args, firstObject);
> while (nextObject = va_arg(args, id)) {
> [arr addObject:nextObject];
> }
>
> va_end(args);
> }
Ignoring, of course, that the example I gave is a very badly-written
initializer that doesn't call [super init] or return self. Please
focus on the use of the va_* macros. :-\
sherm--
--
Cocoa programming in Perl:
http://camelbones.sourceforge.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden