SEL 與 @selector
在 Objective-C 中我們可以使用 @selector
將一個(gè)方法轉(zhuǎn)換并賦值給一個(gè) SEL
類型拷获。SEL
就是對(duì)方法的一種包裝改艇,@selector
就是取類方法的編號(hào)呼巴,他的行為基本可以等同 C 語言的中函數(shù)指針喉誊,只不過 C 語言中,可以把函數(shù)名直接賦給一個(gè)函數(shù)指針寸认,而 Objective-C 的類不能直接應(yīng)用函數(shù)指針,需要一個(gè) @selector
語法來取串慰。
- (void)testMethod {
}
- (void)testMethodWithName:(NSString *) name {
}
SEL method1 = @selector(testMethod);
SEL method2 = @selector(testMethodWithName:);
//也可以使用 NSSelectorFromString
SEL method3 = NSSelectorFromString(@"testMethod");
SEL method4 = NSSelectorFromString(@"testMethodWithName:");
@selector
使用起來更方便废麻,但是 NSSelectorFromString
更加靈活(在運(yùn)行時(shí)動(dòng)態(tài)生成字符串,從而通過方法的名字來調(diào)用對(duì)應(yīng)的方法)模庐。
Selector 與 #selector
在 swift 中并SEL
也通過結(jié)構(gòu)體 Selector
來替代。但使用 Selector
會(huì)提示這樣的警告油宜。
Use '#selector' instead of explicitly constructing a 'Selector'
使用 #selector
的好處是不再需要使用字符串來構(gòu)造掂碱。因?yàn)楫?dāng)使用字符串構(gòu)造時(shí),若傳入的字符串沒有對(duì)應(yīng)的方法名慎冤,那么程序在執(zhí)行時(shí)就會(huì)直接崩潰疼燥。unrecognized selector sent to instance
@objc func testMethod() {
print(#function)
}
@objc func testMethodWithBtn(btn: UIButton) {
print(btn.titleLabel?.text)
}
let testMethod1 = Selector("testMethod")
let testMethod2 = #selector(testMethod)
let testMethod3 = #selector(testMethodWithBtn(btn:))
let btn = UIButton(frame: CGRect(x:0,y:0,width:200,height:50))
btn.backgroundColor = UIColor.red
btn.setTitle("selector", for: .normal)
btn.addTarget(self, action: testMethod3, for: .touchUpInside)
self.view.addSubview(btn)
當(dāng)存在歧義的相同方法名時(shí),可以使用強(qiáng)制類型轉(zhuǎn)換來解決蚁堤。
@objc func testMethod() {
print(#function)
}
@objc func testMethodWithBtn(btn: UIButton) {
print(btn.titleLabel?.text)
}
@objc func testMethodWithBtn(str: String) {
print(str)
}
let testMethod4 = #selector(testMethod as () -> ())
let testMethod5 = #selector(testMethodWithBtn as (UIButton) -> ())
btn.addTarget(self, action: testMethod5, for: .touchUpInside)
#selector
使用起來更加安全和方便醉者,但是 Seletcor
也有存在的必要性。就是當(dāng)我們需要調(diào)用標(biāo)準(zhǔn)庫中的私有方法時(shí)披诗,只能通過字符串來構(gòu)造撬即。
extension UIViewController {
@objc private func privateMethod() {
print(#function)
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let testMethod6 = #selector(privateMethod)
btn.addTarget(self, action: testMethod6, for: .touchUpInside)
}
}
如果使用 #selector
來構(gòu)造方法會(huì)報(bào)錯(cuò)'privateMethod' is inaccessible due to 'private' protection level
這種情況就只能使用 Selector
字符串來構(gòu)造了。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let testMethod7 = Selector("privateMethod")
btn.addTarget(self, action: testMethod7, for: .touchUpInside)
}
}