Objective c implement method which takes array of arguments

后端 未结 3 1148
闹比i
闹比i 2021-02-09 04:19

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         


        
3条回答
  •  深忆病人
    2021-02-09 05:14

    Multiple arguments (also known as an arglist) can only come at the end of a method declaration. Your doSomething method would look something like this:

    + (void)doNumberOfTimes:(NSInteger)numberOfTimes withStrings:(id)firstArg, ...
    {
        va_list args;
        va_start(args, firstArg);
    
        NSString * argString = firstArg;
        while (argString != nil)
        {
            // do something with argString here
    
            argString = va_arg(args, NSString *);
        }
    
        va_end(args);
    }
    

    To be called as follows:

    [SomeClass doNumberOfTimes:2 withStrings:@"A", @"B", nil];
    

    See also: How to create variable argument methods in Objective-C

提交回复
热议问题