Henny Lee Henny Lee - 28 days ago 6
Swift Question

Using method in protocol

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?

Answer

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

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