Re: indeterminate number of arguments
Re: indeterminate number of arguments
- Subject: Re: indeterminate number of arguments
- From: "Sean McBride" <email@hidden>
- Date: Wed, 16 Feb 2011 18:18:48 -0500
- Organization: Rogue Research Inc.
On Wed, 16 Feb 2011 15:12:34 -0800, Todd Heberlein said:
>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, and how, in the
>implementation, do I access the set of arguments passed in?
Todd,
Google for va_start, va_arg, and va_end.
Here's an NSArrayController category method of ours as an example:
- (void)setSortDescriptorsUsingKeys:(NSString*)inFirstKey, ...
{
if (inFirstKey)
{
// We'll create an array out of the varargs.
NSMutableArray* sortDescriptors = [NSMutableArray array];
// Scan all the arguments and add them to the array of sort
descriptors. Note that the first element is not in the argument list.
NSString* nextKey = inFirstKey;
va_list argumentList;
va_start(argumentList, inFirstKey);
do
{
NSSortDescriptor* sd = [[NSSortDescriptor alloc] initWithKey:nextKey
ascending:YES];
[sortDescriptors addObject:sd];
}
while ((nextKey = va_arg(argumentList, NSString*)));
va_end(argumentList);
[self setSortDescriptors:sortDescriptors];
}
}
--
____________________________________________________________
Sean McBride, B. Eng email@hidden
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
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