2017-12-27 203 views
0

出於某種原因,因爲我更新我碰到下面的錯誤與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") 
    } 
} 
+0

好吧,錯誤消息告訴你所有你需要知道的:你的類不符合'MCSessionDelegate' - 要麼使用自動完成,要麼手動檢查'MessageServiceManager'從協議中錯過了什麼 –

+0

沒有錯過那個問題 – SemAllush

回答

0

當提的是:

類型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... 
} 
3

你有一個雙重問號'??'在其中一種方法中。

所以更改此設置:

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之後的字符。