Xcode 7.3 中#selector 指令的使用

Usage of #selector directive in Xcode 7.3

我有协议

protocol AnnotationTapDelegate: AnyObject {

    /* Delegate to identify the tap on Annotation view */
    func didTapAnnotation(sender: UITapGestureRecognizer)
}

和一个class

class CustomAnnotationView: MKAnnotationView {
    func setTapDelegate(delegate: AnnotationTapDelegate!) {
        let tapGesture = UITapGestureRecognizer(target: delegate, action: #selector(AnnotationTapDelegate.didTapAnnotation(_:)))  <== Error
        self.addGestureRecognizer(tapGesture)
    }
}

这给了我编译错误说 "Argument of '#selector' refers to a method that is not exposed in Objective-C" 并给出了 "Add '@obj-c' to expose this to Objective-C" 的建议。在添加 '@obj-c' 之后,我得到了同样的错误并且再次添加了 '@obj-c'。问题没有得到解决。

我在 Xcode 7.3.1 工作。

之前我有

let tapGesture = UITapGestureRecognizer(target: delegate, action: "didTapAnnotation:")

工作正常。将我的 Xcode 更新到 7.3.1 后,我遇到了这个问题。

如何将协议的功能设置为选择器?

您需要在协议层添加@objc。

@objc
protocol AnnotationTapDelegate: AnyObject {

    /* Delegate to identify the tap on Annotation view */
    func didTapAnnotation(sender: UITapGestureRecognizer)
}