swift 中的 Selector

SEL 与 @selector

在 Objective-C 中我们可以使用 @selector 将一个方法转换并赋值给一个 SEL 类型。SEL 就是对方法的一种包装,@selector 就是取类方法的编号,他的行为基本可以等同 C 语言的中函数指针,只不过 C 语言中,可以把函数名直接赋给一个函数指针,而 Objective-C 的类不能直接应用函数指针,需要一个 @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 更加灵活(在运行时动态生成字符串,从而通过方法的名字来调用对应的方法)。

Selector 与 #selector

在 swift 中并SEL 也通过结构体 Selector 来替代。但使用 Selector 会提示这样的警告。 Use '#selector' instead of explicitly constructing a 'Selector'

使用 #selector 的好处是不再需要使用字符串来构造。因为当使用字符串构造时,若传入的字符串没有对应的方法名,那么程序在执行时就会直接崩溃。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)
复制代码

当存在歧义的相同方法名时,可以使用强制类型转换来解决。

 @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 也有存在的必要性。就是当我们需要调用标准库中的私有方法时,只能通过字符串来构造。

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 来构造方法会报错'privateMethod' is inaccessible due to 'private' protection level 这种情况就只能使用 Selector 字符串来构造了。

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let testMethod7 = Selector("privateMethod")

        btn.addTarget(self, action: testMethod7, for: .touchUpInside)
    }
}

复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值