Henny Lee Henny Lee - 11 months ago 76
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() {

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


But this is giving me the error where im creating the

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
method in the view controller?

Answer Source

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

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