一、前言 — 被忽略的不定參數(shù)
前段時間開發(fā)過程中,翻看公司項目里面的舊代碼,看到一個繼承UIAlertView
的控件,里面的初始方法的實現(xiàn)是這樣的:
再來看一下UIAlertView
的初始方法
//UIAlertView初始方法
- (instancetype)initWithTitle:(nullable NSString *)title
message:(nullable NSString *)message
delegate:(nullable id /*<UIAlertViewDelegate>*/)delegate
cancelButtonTitle:(nullable NSString *)cancelButtonTitle
otherButtonTitles:(nullable NSString *)otherButtonTitles, ...
兩個初始方法幾乎是一樣的,看得出來主要目的是為了給點擊事件添加一個Block回調(diào),其中關(guān)鍵的不同點就是最后的otherButtonTitles
,一個后面加了,...
,一個是普通參數(shù)。
沒有了這個,...
,otherButtonTitles
就只能傳一個參數(shù),失去了原有UIAlertView
多個按鈕的實現(xiàn)。
這個,...
就是不定參數(shù),這是C語言開發(fā)里很常用到的傳參方式,但是因為挺多iOS開發(fā)者之前比較少做C語言相關(guān)開發(fā),所以挺多人忽略了這個不定參數(shù)的用法;
二、不定參數(shù)的實現(xiàn)
接下來看一下怎么實現(xiàn)帶有不定參數(shù),...
的方法。
因為這個不是數(shù)組,所以不能用普通的數(shù)組遍歷,得用到va_list :
//帶有不定參數(shù)的初始方法
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
clickButton:(AlertBlock)block
otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
if (self) {
self.block = block;
va_list args;
va_start(args, otherButtonTitles);
if (otherButtonTitles)
{
NSString *otherString;
while ((otherString = va_arg(args, NSString *)))
{
[self addButtonWithTitle:otherString];
}
}
va_end(args);
}
return self;
}
三、va_list和相關(guān)原理
VA_LIST
是在C語言中解決變參問題的一組宏,變參問題是指參數(shù)的個數(shù)不定,可以是傳入一個參數(shù)也可以是多個。
相關(guān)宏有: va_list、va_start、va_arg、va_end
1、va_list 定義一個va_list變量,本質(zhì)上是一個指針。]
va_list args;
2、va_star初始化前面定義的va_list變量,讓指針一開始指向首個參數(shù)
va_start(args, otherButtonTitles);
3、進行一個循環(huán),通過va_arg不停的移動指針,依次取出后面的參數(shù)
NSString *otherString;
while ((otherString = va_arg(args, NSString *)))
{
[self addButtonWithTitle:otherString];
}
4、va_end 與 va_star對應(yīng),結(jié)束參數(shù)的獲取
注意: 最后一個參數(shù)必須為空,不然會出錯。
四、總結(jié)
蘋果UIKit提供了很多帶有不定參數(shù)的方法例如UIActionSheet
等的初始方法,我們在封裝自己的控件、工具類時,模仿官方風格,別人在使用這些控件和工具時學習根本更低,代碼也更為規(guī)范。
我已經(jīng)把相關(guān)的代碼發(fā)在我的Github上,WXSAlertView