Swift - MultipeerConnectivity 类型不符合协议
Swift - MultipeerConnectivity Type does not conform to protocol
我正在尝试 this tutorial 并且遇到了一些代码问题:
class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
我收到一个错误:
Type 'MPCManager' does not comform to protocol 'MCSessionDelegate'
Type 'MPCManager' does not comform to protocol 'MCNearbyServiceBrowserDelegate'
Type 'MPCManager' does not comform to protocol 'MCNearbyServiceAdvertiserDelegate'
但是我下载了示例文件却没有出现同样的错误。我错过了哪一部分?
如果您想遵守这些协议,您需要实现 MCSessionDelegate
和 MCNearbyServiceBrowserDelegate
和 MCNearbyServiceAdvertiserDelegate
的所有必需委托方法。
您必须添加该协议所有必需的委托方法。
我已经在下面的代码中添加了所有需要的方法:
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 种方法添加到您的 class 中以符合协议。
希望对您有所帮助。
您需要执行代表的协议。你可以在 xcode.
中键入函数时找到它
我正在尝试 this tutorial 并且遇到了一些代码问题:
class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
我收到一个错误:
Type 'MPCManager' does not comform to protocol 'MCSessionDelegate'
Type 'MPCManager' does not comform to protocol 'MCNearbyServiceBrowserDelegate'
Type 'MPCManager' does not comform to protocol 'MCNearbyServiceAdvertiserDelegate'
但是我下载了示例文件却没有出现同样的错误。我错过了哪一部分?
如果您想遵守这些协议,您需要实现 MCSessionDelegate
和 MCNearbyServiceBrowserDelegate
和 MCNearbyServiceAdvertiserDelegate
的所有必需委托方法。
您必须添加该协议所有必需的委托方法。
我已经在下面的代码中添加了所有需要的方法:
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 种方法添加到您的 class 中以符合协议。
希望对您有所帮助。
您需要执行代表的协议。你可以在 xcode.
中键入函数时找到它