类型'UIViewController'不符合协议'WCSessionDelegate'

时间:2016-06-18 10:13:20

标签: ios swift watchkit watchconnectivity watch-os-3

自从在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类中。

3 个答案:

答案 0 :(得分:16)

使用Swift 3,您应该根据新协议实现此方法

  

会话:activationDidCompleteWithState:错误:

     

sessionDidBecomeInactive:

     

sessionDidDeactivate:

因为它们在协议上不再标记为可选。

答案 1 :(得分:7)

每个协议都附带了一组您应该实现的方法以符合它们。您必须在类中编写这些方法以符合它。

例如,在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做任何事情

编辑此处:这是apple documentation says

  

此协议的大多数方法都是可选的。您可以实施响应应用程序支持的数据传输操作所需的方法。但是,应用程序应实现对会话的支持:activationDidCompleteWithState:error:支持异步激活的方法,并且iPhone应用程序中的委托应实现sessionDidBecomeInactive:和sessionDidDeactivate:方法以支持多个Apple Watch。

答案 2 :(得分:-1)

在CloudViewController中添加此方法

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

此错误建议您需要为WCSessionDelegate

实现所需的协议方法