Original address: iOS variable parameter (va_list,va_start,va_end)
For example: The Otherbuttontitles in Uialertview's Init method: (NSString *) Otherbuttontitles, ... and more variable parameters.
The way that iOS implements multiple parameters with indefinite length is to use va_list. Va_list is a method provided by C language for processing variable length parameters. Add nil at the end of the argument when calling. The use of va_list requires attention:
(1) First, define the variable of va_list type in the function, this variable is a pointer to the parameter;
(2) Then initialize the va_list variables just defined with Va_start;
(3) Then return the variable parameter with Va_arg, and the second parameter of the va_arg is the type of the parameter you want to return. If the function has more than one variable parameter, call Va_arg to get each parameter in turn;
(4) Finally using the Va_end macro to end the acquisition of variable parameters.
+ (void) functionname: (nsobject*) string, ...
{
va_list args;
Va_start (args, string);
if (string)
{
nsstring *otherstring;
while ((otherstring = Va_arg (args, NSString *))
{
//Get all Parameters
}
va_end (args) sequentially;
}
-(ID) initwithviewcontrollers: (uiviewcontroller<xlswipecontainerchilditem> *) Firstviewcontroller, ...
{self = [self initwithnibname:nil bundle:nil];
if (self) {self.navigationBar.tintColor = [uicolor Whitecolor];
Self.navigationBar.barTintColor = Ioslxsystemcolor;
Self.navigationBar.barStyle = Uibarstyleblack;
ID eachobject;
Va_list argumentlist;
Nsmutablearray * Mutablearray = [[Nsmutablearray alloc] init]; if (Firstviewcontroller)//The argument isn ' t part of the VarArgs list, {
So we ' ll handle it separately.
[Mutablearray Addobject:firstviewcontroller]; Va_start (ArgumentList, Firstviewcontroller);
Start scanning for arguments after Firstviewcontroller.
while ((Eachobject = Va_arg (argumentlist, IDS))//As many times as we can get a argument of type "id" [Mutablearray Addobject:eachobject];
That's isn ' t nil, add it to self ' contents.
Va_end (argumentlist); } Xlswipecontainercontroller * Containercontroller = [[Xlswipecontainercontroller alloc] Initwithviewcontrollers:m
Utablearray];
[Self setviewcontrollers:@[containercontroller]];
return self; }
Description
Va_list args:
Defines a pointer to a variable-number argument list;
Va_start (args,string)://string is the first parameter, which is known as the rightmost parameter, Here is the address to get the first optional argument. Causes the argument list pointer to be the first optional argument in the Function argument list, and the order of the parameters in the function argument list in memory is the same as the order of the function declaration.
Va_arg (args,nsstring): Returns the argument that the pointer refers to in the argument list, returns the type NSString, and causes the parameter pointer to point to the next parameter in the argument list.
A_end (args): empty argument list, and set parameter pointer args invalid