使用协议中的方法

Using method in protocol

我已经创建了协议和协议的扩展:

protocol SomeProtocol: class {
    var someView: UIView { get set }

    func handlePan(recognizer: UIPanGestureRecognizer)
}

extension SomeProtocol where Self: UIViewController {
    func handlePan(recognizer: UIPanGestureRecognizer) {
        // implementation
    }
}

class SomeViewController: UIViewController, SomeProtocol {

    var someView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        someView.frame = CGRect(x: 100, y: 200, width: 50, height: 50)
        someView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan)))
        someView.backgroundColor = .black

        view.addSubview(someView)
    }
}

但这给了我创建 UIPanGestureRecognizer:

的错误

Error: Argument of '#selector' refers to instance method 'handlePan(recogniser:)' that is not exposed to Objective-C

有没有办法解决这个问题而不是在视图控制器中添加 handlePan 方法?

您需要使用 @objc 注释您的协议以将其暴露给 Objective-C 运行时库:

@objc protocol SomeProtocol: class { //...