Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c implement method which takes array of arguments

Hee

Does anybody know how to implement an method in objective c that will take an array of arguments as parameter such as:

[NSArray arrayWithObjects:@"A",@"B",nil];

The method declaration for this method is:

+ (id)arrayWithObjects:(id)firstObj...

I can't seem to make such method on my own. I did the following:

+ (void) doSometing:(id)string manyTimes:(NSInteger)numberOfTimes;

[SomeClass doSometing:@"A",@"B",nil manyTimes:2];

It will give the warningtoo many arguments to function 'doSometing:manyTimes:'

Thanks already.

like image 340
Mats Stijlaart Avatar asked Mar 10 '11 14:03

Mats Stijlaart


People also ask

How do you pass an array to a function in Objective-C?

To pass an array to the function we can declare a formal parameter as a pointer. Example: ObjectiveC.

How do you pass an array of objects as an argument?

Passing array of objects as parameter in C++ It can be declared as an array of any datatype. Syntax: classname array_name [size];

Can a function take an array as an argument?

If we pass an entire array to a function, all the elements of the array can be accessed within the function. Single array elements can also be passed as arguments. This can be done in exactly the same way as we pass variables to a function.

When an array is passed as an argument to a method?

You can pass arrays to a method just like normal variables. When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference). Therefore, any changes to this array in the method will affect the array.


1 Answers

The ellipsis (...) is inherited from C; you can use it only as the final argument in a call (and you've missed out the relevant comma in your example). So in your case you'd probably want:

+ (void)doSomethingToObjects:(id)firstObject, ...;

or, if you want the count to be explicit and can think of a way of phrasing it well:

+ (void)doManyTimes:(NSInteger)numberOfTimes somethingToObjects:(id)firstObject, ...;

You can then use the normal C methods for dealing with ellipses, which reside in stdarg.h. There's a quick documentation of those here, example usage would be:

+ (void)doSomethingToObjects:(id)firstObject, ...
{
    id object;
    va_list argumentList;

    va_start(argumentList, firstObject);
    object = firstObject;

    while(1)
    {
        if(!object) break; // we're using 'nil' as a list terminator

        [self doSomethingToObject:object];
        object = va_arg(argumentList, id);
    }

    va_end(argumentList);
}

EDIT: additions, in response to comments. You can't pass the various things handed to you in an ellipsis to another function that takes an ellipsis due to the way that C handles function calling (which is inherited by Objective-C, albeit not obviously so). Instead you tend to pass the va_list. E.g.

+ (NSString *)doThis:(SEL)selector makeStringOfThat:(NSString *)format, ...
{
    // do this
    [self performSelector:selector];

    // make string of that...

    // get the argument list
    va_list argumentList;
    va_start(argumentList, format);

    // pass it verbatim to a suitable method provided by NSString
    NSString *string = [[NSString alloc] initWithFormat:format arguments:argumentList];

    // clean up
    va_end(argumentList);

    // and return, as per the synthetic example
    return [string autorelease];
}
like image 100
Tommy Avatar answered Nov 08 '22 08:11

Tommy