为什么这个swift代码错了?

出于某种原因,因为我更新我碰到下面的错误与MultiPeer connectivty工作时,我似乎无法弄清楚:为什么这个swift代码错了?

“类型MessageServiceManager不符合协议MCSession”

一切都过去完美,但现在出于某种原因,一切都破裂了,我不知道为什么。 这是它让所有调动起来代码:

extension MessageServiceManager : MCSessionDelegate { 

func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {

NSLog("%@", "peer \(peerID) didChangeState: \(state.stringValue())")

self.delegate?.connectedDevicesChanged(self, connectedDevices: session.connectedPeers.map({$0.displayName}))

}

func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {

NSLog("%@", "didReceiveData: \(data)")

let str = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String

self.delegate?.messageChanged(self, messageString: str)

}

func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {

NSLog("%@", "didReceiveStream")

}

func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL??, withError error: Error?) {

NSLog("%@", "didFinishReceivingResourceWithName")

}

func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {

NSLog("%@", "didStartReceivingResourceWithName")

}

}

回答:

当提的是:

类型MessageServiceManager不符合协议MCSession

这意味着,你是您班上的所有必需方法均符合MCSessionDelegate。

要解决这个问题,您可以按指令并点击MCSessionDelegate,然后选择“Jump to Definition”。应该引导你:

// Delegate methods for MCSession. 

public protocol MCSessionDelegate : NSObjectProtocol {

// Remote peer changed state.

@available(iOS 7.0, *)

public func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState)

// Received data from remote peer.

@available(iOS 7.0, *)

public func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID)

// Received a byte stream from remote peer.

@available(iOS 7.0, *)

public func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID)

// Start receiving a resource from remote peer.

@available(iOS 7.0, *)

public func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress)

// Finished receiving a resource from remote peer and saved the content

// in a temporary location - the app is responsible for moving the file

// to a permanent location within its sandbox.

@available(iOS 7.0, *)

public func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?)

// Made first contact with peer and have identity information about the

// remote peer (certificate may be nil).

@available(iOS 7.0, *)

optional public func session(_ session: MCSession, didReceiveCertificate certificate: [Any]?, fromPeer peerID: MCPeerID, certificateHandler: @escaping (Bool) -> Swift.Void)

}

这是协议的声明。

现在您可以识别您必须实现的方法(所有非可选方法)或方法签名是否匹配;你会注意到:

session(_:didFinishReceivingResourceWithName:fromPeer:at:withError:)

,因为它应该是让来抱怨的编译器的原因,实现明明所有你需要做的就是实现它为:

func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) { 

// your logic goes here...

}

回答:

你有一个双重问号'??'在其中一种方法中。

所以更改此设置:

func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL??, withError error: Error?) { 

NSLog("%@", "didFinishReceivingResourceWithName")

}

这样:

func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) { 

NSLog("%@", "didFinishReceivingResourceWithName")

}

注意去除的 '?' URL之后的字符。

以上是 为什么这个swift代码错了? 的全部内容, 来源链接: utcz.com/qa/264986.html

回到顶部