选择器与动作Swift 4

use*_*882 12 selector ios swift swift4

Swift的新手.我下面有两个片段:

NotificationCenter.default.addObserver(self, 
    selector:#selector(ViewController.notificationReceived), 
    name: Notification.Name(rawValue: name), object: nil)

@objc func notificationReceived(notification:Notification){
    let x = notification.userInfo!
    print("\(x["name"]!)")

}
Run Code Online (Sandbox Code Playgroud)

最后

let x:UITapGestureRecognizer = UITapGestureRecognizer(target: self, 
    action: #selector(tapped))

self.addGestureRecognizer(x)

func tapped(){
    print("tapped")

    self.delegate!.theViewTapped()

}
Run Code Online (Sandbox Code Playgroud)

为什么这个notificationCenter?我应该提供参数的@objc标签,selector但不提供UITapGestureRecognizer动作参数?

SelectorAction inSwift有什么区别?

OOP*_*Per 9

检查此提案的Swift 4: SE-0160限制@objc推理

根据提案中的说明,您的第二个代码段也需要@objc.

事实上,与Xcode 9 beta2捆绑在一起的Swift 4编译器使用以下方法为该行生成此错误#selector(tapped):

错误:'#selector'的参数是指未暴露给Objective-C的实例方法'tapped()'

注意:添加'@objc'以将此实例方法公开给Objective-C

也许你的第二个有点太旧了,无法使用Swift 4.你最好认为通过选择器调用的所有方法都需要@objc属性.