繁体   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