簡體   English   中英

類型'UIViewController'不符合協議'WCSessionDelegate'

[英]Type 'UIViewController' does not conform to protocol 'WCSessionDelegate'

自從在Xcode 8(Beta 1)和Swift 3上升級后,我在此行中出錯:

class CloudViewController: UIViewController, WCSessionDelegate {

它說 :

類型'UIViewController'不符合協議'WCSessionDelegate'

這是我的(使用Xcode 7和Swift 2)代碼:

override func viewDidLoad() {
    super.viewDidLoad()

    if(WCSession.isSupported()){
        self.session = WCSession.default()
        self.session.delegate = self
        self.session.activate()
    }
}

func session(_ session: WCSession, didReceiveMessage message: [String : AnyObject]) {

    print("didReceiveMessage")

    watchMessageHandler.getMessage(message)

}

此錯誤也會顯示在WKInterfaceController類中。

使用Swift 3,您應該根據新協議實現此方法

會議:activationDidCompleteWithState:錯誤:

sessionDidBecomeInactive:

sessionDidDeactivate:

因為它們不再在協議上標記為可選。

每個協議都帶有一組您應該實現的方法,以便符合它們。 您必須在類中編寫這些方法以符合它。

例如,在UIViewController中,如果您決定使用tableView,則必須添加UITableViewDataSourceUITableViewDelegate協議,如下所示:

class ViewController : UIViewController, UITableViewDataSource, UITableViewDelegate  {

}

但是,這不是協議的完整實現。 這僅僅是宣言。

要讓View Controller符合協議,您必須實現兩個方法,即: cellForRowAtIndexPathnumberOfRowsInSection 這是協議的要求。

因此,完整的實現看起來像:

class ViewController : UIViewController, UITableViewDataSource, UITableViewDelegate  {

     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

         let cell = tableView.dequeueReusableCellWithIdentifier("cellID", forIndexPath: indexPath) as! ExperienceCell

         return cell
     }

     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
     }

}

因此,您必須查看文檔並找到協議要求類實現的方法。 那應該可以解決這個問題。 我認為不要對Xcode 8或swift 3做任何事情

編輯 :這是蘋果文檔所說的

該協議的大多數方法都是可選的。 您可以實施響應應用程序支持的數據傳輸操作所需的方法。 但是,應用程序應實現對會話的支持:activationDidCompleteWithState:error:支持異步激活的方法,並且iPhone應用程序中的委托應實現sessionDidBecomeInactive:和sessionDidDeactivate:方法以支持多個Apple Watches。

在CloudViewController中添加此方法

internal func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: NSError?){
}

此錯誤建議您需要為WCSessionDelegate實現所需的協議方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM