Using method in protocol

Henny Lee

I've created a protocol and an extension of the 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)
    }
}

But this is giving me the error where im creating the UIPanGestureRecognizer:

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

Is there a way to fix this instead of adding handlePan method in the view controller?

Alexander

You need to annotate your protocol with @objc to expose it to the Objective-C runtime library:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related