Understanding Swift 2.2 Selector Syntax - #selector()

前端 未结 4 1660
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 02:13

I am switching over the syntax of my project toward Swift 2.2 (which xCode helps me do automatically); however, I do not understand the new #selector() syntax.<

4条回答
  •  不思量自难忘°
    2020-12-08 02:52

    Swift 2.2 has deprecated Stringified selectors: In swift 2.0, we use to write the selector as a String i.e "buttonClicked". The disadvantage with this approach is that the compiler can't check whether the method really exists or not at compile time(Even if you have misspelled it).

    EX:1

    func buttonClicked(){
    }
    

    So the above method in the new approach can be called as #selector(buttonClicked)

    EX:2

    func buttonClicked(stringValue : String){
    }
    

    So the above method in the new approach can be called as #selector(buttonClicked(_:))

    EX:3

    func buttonClicked(stringValue : String, indexValue : Int){
    }
    

    So the above method with parameters in the new approach can be called as #selector(buttonClicked(_:indexValue:))

提交回复
热议问题