• 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: Variable Number of Parameters in Method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Variable Number of Parameters in Method
      • From: Alex Zavatone <email@hidden>
    • Re: Variable Number of Parameters in Method
      • From: Dave <email@hidden>
References: 
 >Nav stack creating a bunch of UIImageViews on top of my view (From: Rick Mann <email@hidden>)
 >Variable Number of Parameters in Method (From: Dave <email@hidden>)

  • Prev by Date: Variable Number of Parameters in Method
  • Next by Date: Re: Drawing after CGContextClosePath() still appends line
  • Previous by thread: Variable Number of Parameters in Method
  • Next by thread: Re: Variable Number of Parameters in Method
  • Index(es):
    • Date
    • Thread