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