类型“类”不符合协议“MCSessionDelegate”
Posted
技术标签:
【中文标题】类型“类”不符合协议“MCSessionDelegate”【英文标题】:Type 'Class' does not conform to protocol 'MCSessionDelegate' 【发布时间】:2018-08-13 02:01:22 【问题描述】:我正在将我的代码从 Swift 2.X 迁移到 3.X,并且已经解决了我的大部分运行时错误,除了一个不断出现的错误。 我拥有所有必需的功能,并且已经清理并删除了派生数据,但它仍然说我的类不符合“MCSessionDelegate”。 `
import MultipeerConnectivity
import Foundation
class Cashier: Advertiser
var waiterBecomesConnectedHandler: ((MCPeerID) -> Void)?
var waiterBecomesDisconnectedHandler: ((MCPeerID) -> Void)?
extension Cashier: MCSessionDelegate
@available(ios 7.0, *)
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress)
@available(iOS 7.0, *)
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState)
switch state
case .notConnected:
waiterBecomesDisconnectedHandler?(peerID)
connectedPeerSessions.removeValue(forKey: peerID)
break
case .connected:
waiterBecomesConnectedHandler?(peerID)
break
case .connecting:
break
@available(iOS 7.0, *)
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID)
@available(iOS 7.0, *)
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID)
@available(iOS 7.0, *)
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL, withError error: Error?)
`
这是协议的代码 `
// 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)
`
【问题讨论】:
您真的在构建应用程序以向后兼容 iOS = 7.0。在大多数情况下,您不应再针对这些旧版本的 iOS。 @ekscrypto 感谢您的建议*。我删除了 @available(...) 并将我的 iOS 目标更改为 8.0,现在它告诉我添加我的代码中已有的didStartReceivingResourceWithName
存根。
它应该在警告气泡中有一个“修复”按钮。如果您单击它检查它创建的存根,很可能您有一个类型错误或参数名称已更改
我点击了修复按钮,它显示的功能完全相同。我错过了什么吗?
【参考方案1】:
从扩展中删除所有功能
extension Cashier: MCSessionDelegate.
您会看到缺少 subs 的错误,点击修复选项,它会自动添加所有需要的功能
如果以上两个步骤都不起作用,请尝试清理构建文件夹并重新启动 Xcode
【讨论】:
感谢您的回答,我执行了上述步骤,但仍然显示我的课程仍然不符合协议。 请分享 MCSessionDelegate 的协议代码或者repo链接 试试下面 Ashley Mills 发布的答案。好像是这个问题【参考方案2】:您的委托方法之一是错误的。 at localURL: URL
应该是 at localURL: URL?
func session(_ session: MCSession,
didFinishReceivingResourceWithName resourceName: String,
fromPeer peerID: MCPeerID,
at localURL: URL?,
withError error: Error?)
https://developer.apple.com/documentation/multipeerconnectivity/mcsessiondelegate/1406984-session
【讨论】:
以上是关于类型“类”不符合协议“MCSessionDelegate”的主要内容,如果未能解决你的问题,请参考以下文章
请帮忙:类型'()'不能符合'View';只有结构/枚举/类类型可以符合协议
协议类型“Encodable”的值不能符合“Encodable”;只有结构/枚举/类类型可以符合协议
使用 if 语句时:类型 '()' 不能符合 'View';只有结构/枚举/类类型可以符合协议