[英]Swift - MultipeerConnectivity Type does not conform to protocol
I am trying out this tutorial and having some problem with the code: 我正在尝试这个教程并且代码有些问题:
class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
I got an error: 我收到一个错误:
Type 'MPCManager' does not comform to protocol 'MCSessionDelegate'
类型'MPCManager'不符合协议'MCSessionDelegate'
Type 'MPCManager' does not comform to protocol 'MCNearbyServiceBrowserDelegate'
类型'MPCManager'不符合协议'MCNearbyServiceBrowserDelegate'
Type 'MPCManager' does not comform to protocol 'MCNearbyServiceAdvertiserDelegate'
类型'MPCManager'不符合协议'MCNearbyServiceAdvertiserDelegate'
But I download the sample file it did not get the same error. 但是我下载了它没有得到相同错误的示例文件。 Which part had I missed out?
我错过了哪一部分?
如果要符合这些协议,则需要实现MCSessionDelegate
和MCNearbyServiceBrowserDelegate
以及MCNearbyServiceAdvertiserDelegate
所需的所有委托方法。
You have to add all the required delegate methods of that protocol. 您必须添加该协议的所有必需委托方法。
I have added all the required method into below code: 我在下面的代码中添加了所有必需的方法:
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)!){
}
}
If you want to check what are the required methods for specific delegate then just command + click on that delegate and you will find all the methods related with that delegate suppose if you command + click on MCSessionDelegate
then you will see something like this: 如果你想检查特定委托所需的方法是什么,那么只需命令+点击该委托就可以找到与该委托相关的所有方法,如果你命令+点击
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)!)
}
Where only last method is optional so if you do not add it your delegate works fine but you have to add all above 5 methods into your class to conform the protocol. 只有最后一个方法是可选的,所以如果你不添加它你的委托工作正常,但你必须将所有上述5个方法添加到你的类中以符合协议。
Hope It will help. 希望它会有所帮助。
You need to implement the protocols of the delegates. 您需要实现委托的协议。 You can find it out when typing functions in xcode.
在xcode中键入函数时可以找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.