Re: Variable Number of Parameters in Method
Re: Variable Number of Parameters in Method
- Subject: Re: Variable Number of Parameters in Method
- From: "Stephen J. Butler" <email@hidden>
- Date: Wed, 21 Aug 2013 03:02:28 -0500
On Wed, Aug 21, 2013 at 2:35 AM, Dave <email@hidden> wrote:
> -(void) methodX:(NSString*) theName,…
> {
> va_list
> myArgumentList;
> NSInteger
> myArgumentCount;
>
> myArgumentCount = 0;
> va_start(myArgumentList,theMethodName);
>
> while(va_arg(myArgumentList,NSString*) != nil)
> //********************
> myArgumentCount++;
>
Whatever else you're trying to do, you cannot write a loop like that.
va_arg() will NOT return nil/NULL/0 when it reaches the end. In fact,
va_arg() has no possible way of knowing when it has reached the end. That's
why you pass in theName; it's 100% up to you to parse theName and figure
out how many arguments were passed in and call va_arg() properly.
Or, alternately, force all of your arguments to be of a common base type
(say, "id") and nil terminate the list. A la +[NSArray arrayWithObjects:],
etc.
But you can't have it both ways.
Once you have that your while() loop is easy:
while (more_arguments) {
argument_type = get_next_arg_type();
if (argument_type == myNSStringType) {
objVal = va_arg(myArgList,NSString*);
} else if (argument_type == myNSIntegerType) {
intVal = va_arg(myArgList,NSInteger);
} ... etc ..
more_arguments = has_more_arguments();
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden