Swift - MultipeerConnectivity 类型不符合协议

Posted

技术标签:

【中文标题】Swift - MultipeerConnectivity 类型不符合协议【英文标题】:Swift - MultipeerConnectivity Type does not conform to protocol 【发布时间】:2015-06-30 09:08:28 【问题描述】:

我正在尝试this tutorial,但代码有问题:

class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate 

我遇到了一个错误:

类型“MPCManager”不符合协议“MCSessionDelegate”

类型“MPCManager”不符合协议“MCNearbyServiceBrowserDelegate”

类型“MPCManager”不符合协议“MCNearbyServiceAdvertiserDelegate”

但是我下载了示例文件却没有出现同样的错误。我错过了哪一部分?

【问题讨论】:

【参考方案1】:

如果你想符合这些协议,你需要实现MCSessionDelegateMCNearbyServiceBrowserDelegateMCNearbyServiceAdvertiserDelegate的所有必需的委托方法。

【讨论】:

【参考方案2】:

您必须添加该协议的所有必需委托方法。

我已将所有必需的方法添加到以下代码中:

import Foundation
import MultipeerConnectivity


class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate 

    //Type 'MPCManager' does not comform to protocol 'MCSessionDelegate'
    // Remote peer changed state
    func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState)

    

    // Received data from remote peer
    func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!)

    

    // Received a byte stream from remote peer
    func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!)

    

    // Start receiving a resource from remote peer
    func session(session: MCSession!, didStartReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!)

    

    // 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
    func session(session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!)

    


    //Type 'MPCManager' does not comform to protocol 'MCNearbyServiceBrowserDelegate'

    // Found a nearby advertising peer
    func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!)

    

    // A nearby peer has stopped advertising
    func browser(browser: MCNearbyServiceBrowser!, lostPeer peerID: MCPeerID!)

    

    //Type 'MPCManager' does not comform to protocol 'MCNearbyServiceAdvertiserDelegate'
    // Incoming invitation request.  Call the invitationHandler block with YES and a valid session to connect the inviting peer to the session.
    func advertiser(advertiser: MCNearbyServiceAdvertiser!, didReceiveInvitationFromPeer peerID: MCPeerID!, withContext context: NSData!, invitationHandler: ((Bool, MCSession!) -> Void)!)

    


如果您想检查特定委托所需的方法,那么只需命令+单击该委托,您将找到与该委托相关的所有方法假设如果您命令+单击MCSessionDelegate,那么您将看到一些东西像这样:

// Delegate methods for MCSession
protocol MCSessionDelegate : NSObjectProtocol 

    // Remote peer changed state
    func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState)

    // Received data from remote peer
    func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!)

    // Received a byte stream from remote peer
    func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!)

    // Start receiving a resource from remote peer
    func session(session: MCSession!, didStartReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!)

    // 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
    func session(session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!)

    // Made first contact with peer and have identity information about the remote peer (certificate may be nil)
    optional func session(session: MCSession!, didReceiveCertificate certificate: [AnyObject]!, fromPeer peerID: MCPeerID!, certificateHandler: ((Bool) -> Void)!)

只有最后一个方法是可选的,所以如果您不添加它,您的委托可以正常工作,但您必须将以上 5 种方法添加到您的类中以符合协议。

希望它会有所帮助。

【讨论】:

【参考方案3】:

您需要实现代表的协议。在 xcode 中输入函数时可以发现。

【讨论】:

我想补充一点,如果您看到报告的错误(不符合),如果您点击红色圆圈,您可以在左侧面板中查看错误并展开错误。然后 Xcode 将指出哪个例程有问题或哪个例程丢失。此外,您可以开始在代码部分输入函数名称,然后会出现声明。

以上是关于Swift - MultipeerConnectivity 类型不符合协议的主要内容,如果未能解决你的问题,请参考以下文章

Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

swift 示例BS swift.swift

swift swift_bug.swift

ios 整理(一)swift和oc的区别

swift swift_extension5.swift

swift swift_optional4.swift