2016-11-11 72 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