简体   繁体   English

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

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

Since upgrading on Xcode 8 (Beta 1) and Swift 3 I have an error in this line: 自从在Xcode 8(Beta 1)和Swift 3上升级后,我在此行中出错:

class CloudViewController: UIViewController, WCSessionDelegate {

It says : 它说 :

Type 'UIViewController' does not conform to protocol 'WCSessionDelegate' 类型'UIViewController'不符合协议'WCSessionDelegate'

This is my (with Xcode 7 and Swift 2 working) code: 这是我的(使用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)

}

This error also shows up in the WKInterfaceController classes. 此错误也会显示在WKInterfaceController类中。

With Swift 3, you should implement this methods according to the new protocol 使用Swift 3,您应该根据新协议实现此方法

session:activationDidCompleteWithState:error: 会议:activationDidCompleteWithState:错误:

sessionDidBecomeInactive: sessionDidBecomeInactive:

sessionDidDeactivate: sessionDidDeactivate:

because they are not marked as optional on protocol anymore. 因为它们不再在协议上标记为可选。

Every protocol comes with a set of methods that you are supposed to implement in order to conform to them. 每个协议都带有一组您应该实现的方法,以便符合它们。 You must write those methods in your class to conform to it. 您必须在类中编写这些方法以符合它。

For example, in a UIViewController, if you decide to have a tableView, you must add the UITableViewDataSource , UITableViewDelegate protocol, like so: 例如,在UIViewController中,如果您决定使用tableView,则必须添加UITableViewDataSourceUITableViewDelegate协议,如下所示:

class ViewController : UIViewController, UITableViewDataSource, UITableViewDelegate  {

}

But, this is not complete implementation of the protocol. 但是,这不是协议的完整实现。 This is mere declaration. 这仅仅是宣言。

To actually have your View Controller conform to the protocol, you will have to implement two methods, namely: cellForRowAtIndexPath and numberOfRowsInSection . 要让View Controller符合协议,您必须实现两个方法,即: cellForRowAtIndexPathnumberOfRowsInSection This is the requirement of the protocol. 这是协议的要求。

So, the complete implementation would look something like: 因此,完整的实现看起来像:

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
     }

}

Therefore, you must look into the documentation and find what methods does your protocol require the class to implement. 因此,您必须查看文档并找到协议要求类实现的方法。 That should solve this problem. 那应该可以解决这个问题。 And I don't think it is to do anything with Xcode 8 or swift 3 我认为不要对Xcode 8或swift 3做任何事情

EDIT Here: This is what apple documentation says 编辑 :这是苹果文档所说的

Most methods of this protocol are optional. 该协议的大多数方法都是可选的。 You implement the methods you need to respond to the data transfer operations that your apps support. 您可以实施响应应用程序支持的数据传输操作所需的方法。 However, apps should implement support for the session:activationDidCompleteWithState:error: method to support asynchronous activation, and the delegate in your iPhone app should implement the sessionDidBecomeInactive: and sessionDidDeactivate: methods to support multiple Apple Watches. 但是,应用程序应实现对会话的支持:activationDidCompleteWithState:error:支持异步激活的方法,并且iPhone应用程序中的委托应实现sessionDidBecomeInactive:和sessionDidDeactivate:方法以支持多个Apple Watches。

Add this Methods in your CloudViewController 在CloudViewController中添加此方法

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

This error suggest you need to implement the required protocol method for WCSessionDelegate 此错误建议您需要为WCSessionDelegate实现所需的协议方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM