ios 可变参数valist,vastart,vaend (例如-(void)addButton:(UIButton *)sender,...{})

本文详细介绍了iOS中如何使用va_list来处理可变参数的方法,包括va_start、va_arg及va_end的使用流程,并通过实例展示了如何在实际代码中应用这些技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E6%B1%87/4403.shtml


"ios 可变参数valist,vastart,vaend":
关键词 ios  可变  参数  valist  vastart  vaend

例如:uialertview的init方法中的otherbuttontitles:(nsstring *)otherbuttontitles, ...等多个可变参数。

ios实现传递不定长的多个参数的方法是使用va_list。va_list是c语言提供的处理变长参数的一种方法。在调用的时候要在参数结尾的时候加nil。va_list的使用需要注意:

(1)首先在函数里定义va_list型的变量,这个变量是指向参数的指针;

(2)然后用va_start初始化刚定义的va_list变量;

(3)然后用va_arg返回可变的参数,va_arg的第二个参数是你要返回的参数的类型.如果函数有多个可变参数的,依次调用va_arg获取各个参数;

(4)最后用va_end宏结束可变参数的获取。

 

 

+ (void)functionname:(nsobject*)string, ...   
{  
    va_list args;  
    va_start(args, string);  
    if (string)   
    {  
        nsstring *otherstring;  
        while ((otherstring = va_arg(args, nsstring *)))   
        {  
            //依次取得所有参数  
        }  
    }  
    va_end(args);  
}  

 

 

-(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 first 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, id))) // as many times as we can get an argument of type "id"
                [mutablearray addobject:eachobject];        // that isn't nil, add it to self's contents.
            va_end(argumentlist);
        }
        xlswipecontainercontroller * containercontroller = [[xlswipecontainercontroller alloc] initwithviewcontrollers:mutablearray];
        [self setviewcontrollers:@[containercontroller]];
        
        
        
    }
    return self;
}

 

说明:

va_list args:

//定义一个指向个数可变的参数列表指针;

va_start(args,string)://string为第一个参数,也就是最右边的已知参数,这里就是获取第一个可选参数的地址.使参数列表指针指向函数参数列表中的第一个可选参数,函数参数列表中参数在内存中的顺序与函数声明时的顺序是一致的。

va_arg(args,nsstring):返回参数列表中指针所指的参数,返回类型为nsstring,并使参数指针指向参数列表中下一个参数。  

a_end(args):清空参数列表,并置参数指针args无效

 



3月 12 15:23:14 ub kernel: proc_thermal_pci 0000:00:04.0: error: proc_thermal_add, will continue 3月 12 15:23:14 ub kernel: 3月 12 23:19:05 ub kernel: iwlwifi 0000:00:14.3: WRT: Invalid buffer destination 3月 12 23:19:05 ub kernel: iwlwifi 0000:00:14.3: WRT: Invalid buffer destination 3月 12 23:19:06 ub kernel: Bluetooth: hci0: Malformed MSFT vendor event: 0x02 3月 12 23:19:08 ub gnome-session-binary[1236]: GLib-GIO-CRITICAL: g_bus_get_sync: assertion 'error == NULL || *error == NULL' failed 3月 12 23:19:08 ub gnome-session-binary[1236]: GLib-GIO-CRITICAL: g_bus_get_sync: assertion 'error == NULL || *error == NULL' failed 3月 12 23:19:12 ub gdm-password][1483]: gkr-pam: unable to locate daemon control file 3月 12 23:19:15 ub systemd[1516]: Failed to start Application launched by gnome-session-binary. 3月 12 23:19:15 ub systemd[1516]: Failed to start Application launched by gnome-session-binary. 3月 12 23:19:18 ub gnome-session-binary[1236]: CRITICAL: gsm_client_peek_id: assertion 'GSM_IS_CLIENT (client)' failed 3月 12 23:19:18 ub gnome-session-binary[1236]: GLib-GObject-CRITICAL: g_signal_emit_valist: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed 3月 12 23:19:18 ub gnome-session-binary[1236]: CRITICAL: gsm_client_peek_id: assertion 'GSM_IS_CLIENT (client)' failed 3月 12 23:19:18 ub gdm-launch-environment][1033]: GLib-GObject: g_object_unref: assertion 'G_IS_OBJECT (object)' failed 3月 12 23:23:45 ub bluetoothd[860]: Failed to set mode: Failed (0x03) ~
最新发布
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值