Objective-c - Passing in variables to a variable-length method

后端 未结 2 1850
长发绾君心
长发绾君心 2021-01-19 07:06

I\'ve got an array with items, and I want to pass these in to a variable-length method. How do you do that?

I.e., I\'ve got this (for example):

NSArr         


        
2条回答
  •  不要未来只要你来
    2021-01-19 07:09

    - (id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles ...
    {
        va_list args;
        va_start(args, otherButtonTitles);
        for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*))
        {
            //do something with nsstring
        }
        va_end(args);
    }
    

    You could also just make an argument in your functions that accepts an array (easy solution)

    Anyway the ... notation is for a variable amount of arguments at the end of a function.

提交回复
热议问题