Re: Variable Number of Parameters in Method
Re: Variable Number of Parameters in Method
- Subject: Re: Variable Number of Parameters in Method
- From: Alex Zavatone <email@hidden>
- Date: Wed, 21 Aug 2013 07:29:02 -0400
However, this does:
@interface NSArray (NSArrayCreation)
+ (id)array;
+ (id)arrayWithObject:(id)anObject;
+ (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
+ (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
arrayWithObjects:
Creates and returns an array containing the objects in the argument list.
+ (id)arrayWithObjects:(id)firstObj, ...
Parameters
firstObj, ...
A comma-separated list of objects ending with nil.
Return Value
An array containing the objects in the argument list.
Discussion
This code example creates an array containing three different types of element:
NSArray *myArray;
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = @"a string";
myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
On Aug 21, 2013, at 4:02 AM, Stephen J. Butler 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
_______________________________________________
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