Swift 知识点1:#selector

2018年新的开始,好好的整理一下Swift知识点,希望在整理过程中提升自己。

1.swift 4.0 取消了 Selector("cyanButtonClick") 这种写法.

class ViewController: UIViewController {

    @IBOutlet weak var buttonA: UIButton!

     override func viewDidLoad() {
        super.viewDidLoad()
        //错误写法
         buttonA.addTarget(self,
                          action: Selector("buttonActionA"),
                          for: .touchUpInside)
         //正确写法
         buttonA.addTarget(self,
                          action: NSSelectorFromString("buttonActionA"),
                          for: .touchUpInside)
    }

    func buttonActionA(){
        print("text for selector")
    }
//以上写法已经被swift4.0取消,如果继续使用会被报“Use '#selector' instead of explicitly constructing a 'Selector' "

2.我们在开发过程中通常会这样写,坦率的讲这样写没什么问题,但是不够优雅

class ViewController: UIViewController {
    @IBOutlet weak var buttonA: UIButton!
     override func viewDidLoad() {
        super.viewDidLoad()
        //防止方法名重复,在方法前面跟上所在类以区别
       buttonA.addTarget(self,
                          action: #selector(ViewController.buttonActionA),
                          for: .touchUpInside)
    }

    func buttonActionA(){
        print("text for selector")
    }

3.如何访问我们的private私有方法,则是在前面加@Objc变成运行时。

class ViewController: UIViewController {
    @IBOutlet weak var buttonA: UIButton!
     override func viewDidLoad() {
        super.viewDidLoad()
        //当然如果不需要区分则可以去掉ViewController,也可以用self代替ViewController
        let methodObjc = #selector(ViewController.buttonActionObjc)
        buttonA.addTarget(self,
                          action: methodObjc,
                          for: .touchUpInside)

    }

    func buttonActionObjc(){
        print("text for selectorObjc")
    }

4.相同方法名不同参数,可以用 as 区分

class ViewController: UIViewController {
    @IBOutlet weak var buttonA: UIButton!
    @IBOutlet weak var buttonB: UIButton!
     override func viewDidLoad() {
        super.viewDidLoad()

      let methodA = #selector(buttonActionA as () -> ())
      let methodB = #selector(buttonActionB as (UIButton) -> ())
        
       buttonA.addTarget(self,
                             action: methodA,
                             for: .touchUpInside)
        buttonB.addTarget(self,
                                    action: methodB,
                                    for: .touchUpInside)
    }

    func buttonActionA(){
        print("text for buttonActionA")
    }

    func buttonActionB(){
        print("text for buttonActionB")
    }

5.Swift 3.0 中加入了 Selector 引用变量(不可为常量)的 getter 和 setter 方法:

相关链接:https://github.com/apple/swift-evolution/blob/master/proposals/0064-property-selectors.md
一直在找使用场景:a.可以判断属性是否包含getter 和 setter。  b.可以结合responds方法使用。
class Person: NSObject {
    dynamic var firstName: String
    dynamic let lastName: String
    dynamic var fullName: String {
        return "\(firstName) \(lastName)"
    }
    
    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }
}
let firstNameGetter = #selector(getter: Person.firstName)
let firstNameSetter = #selector(setter: Person.firstName)

6.比较优雅的写法可以把Selector定义成extension,包含所有定义的selector

extension Selector {

    static let methodA = #selector(ViewController.buttonActionA)

}

class ViewController: UIViewController {
    @IBOutlet weak var buttonA: UIButton!
     override func viewDidLoad() {
        super.viewDidLoad()

        buttonA.addTarget(self,
                          action: .methodA,
                          for: .touchUpInside)
    }

    func buttonActionA(){
        print("text for selector")
    }

你可能感兴趣的:(Swift 知识点1:#selector)