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)
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,776评论 0 9
  • 由于种种原因,简书等第三方平台博客不再保证能够同步更新,欢迎移步 GitHub:https://github.co...
    萌面大道阅读 36,119评论 19 30
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,232评论 0 7
  • 最近在看《Swifter - 100 个 Swift 必备 tips》这本书。这里记录一些思考🤔。首先一个就是第一...
    周二可阅读 858评论 0 0
  • 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的...
    西木阅读 30,638评论 33 466