Re: Variable Number of Parameters in Method
Re: Variable Number of Parameters in Method
- Subject: Re: Variable Number of Parameters in Method
- From: Dave <email@hidden>
- Date: Wed, 21 Aug 2013 10:22:20 +0100
Hi,
Sorry that snippet was from an existing method that used a nil terminated list, you're right, I can't do that in this case.
I was thinking that it would be easier if everything was an Object, the types I need to support are:
id
Numeric types, signed/unsigned integers of different lengths and floats.
So I could convert anything that is not an object into an NSNumber and numberWithXXX, which looks like the best way to go!
Doing it the other way, I know the number of parameters from "theName".
Rewriting the original code:
-(void) methodX:(NSString*) theName,…
{
va_list myArgumentList;
NSInteger myArgumentCount;
NSInteger myIndex;
NSArray * myTypeArray;
NSString* myType;
NSInteger myNSInteger;
CGFloat myCGFloat;
NSMutableString* myFormattedString;
// returns an array of types, index 0 = parameter 0 in the va_list as so:
// NSString*, CGFloat,NSInteger,
myFormattedString = [[NSMutableString alloc] initWithString:@""];
myTypeArray = [self getTypeArrayForKey:theName];
myArgumentCount = [myTypeArray count];
va_start(myArgumentList,theMethodName);
for (myIndex = 0; myIndex < myArgumentCount; myArgumentCount++)
{
myType = [myTypeArray objectAtIndex: myIndex];
if ([myType isEqualToString:@"NSInteger"] )
{
myNSInteger = va_arg(myArgumentList,NSInteger*);
// do something with myNSInteger
[myFormattedString appendFormat:@"myNSInteger = %d ", myNSInteger];
}
else if ([myType isEqualToString:@"CGFloat"] )
{
myCGFloat = va_arg(myArgumentList, CGFloat*);
// do something with myCGFloat
[myFormattedString appendFormat:@"myCGFloat = %f ", myCGFloat];
}
else if ([myType isEqualToString:@"XXXXX"] )
…..
…..
…..
}
va_end(myArgumentList);
}
Thanks a lot for your help.
Cheers
Dave
On 21 Aug 2013, at 09:02, "Stephen J. Butler" <email@hidden> wrote:
> 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