iOS variable parameters (va_list,va_start,va_end)

Source: Internet
Author: User
Tags argumentlist

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.