2016-11-11 32 views
0

使用方法,我创建了一个协议,该协议的扩展:在协议

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) 
    } 
} 

但是,这是给我的错误的IM创造UIPanGestureRecognizer

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

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

+0

由于该方法需要签名应该是'#selector参数(handlePan(_ :) )' – vadian

+0

@vadian实际上,正式签名将是'#selector(handlePan(recognitionizer :))' - 尽管'handlePan'是明确的,'#selector(handlePan)'是完全可以接受的。 – Hamish

回答

0

您需要@objc来注释协议将其暴露在Objective-C的运行时库:

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

现在Xcode抱怨'非'@ objc'属性...不符合我的其他属性'@objc'协议'的要求。有没有另外的方法,而不必添加'@ objc'? –

+0

不,你正在试图在你的Swift协议中使用Objective C运行时的一个特性。该协议需要暴露于Objective C运行时。你必须排除错误。在SO上搜索,或者根据需要发布新问题。 – Alexander

+2

是的,现在的问题是Objective-C无法看到协议扩展;) – Hamish