swift 2 TapGestureRecognizer 不接受自己作为目标

swift 2 TapGestureRecognizer doesn't accept self as a target

我正在做一个项目,然后我需要添加一个 tapGesture,问题是我无法将自己添加为目标,我不断收到此错误:

Cannot convert value of type 'NSObject -> () -> ViewController' to expected argument type 'AnyObject?'

这是我的代码:

我也试过了:

let tap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")

但我得到了同样的错误

将该行放在 viewDidLoad 而不是 class 级别:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let tap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
}

根据我在您编辑问题时看到的内容,您的 tap 变量超出范围。