Type 'Class' does not conform to protocol 'MCSessionDelegate'

196 Views Asked by At

I'm working on migrating my code from Swift 2.X to 3.X and have already resolved majority of my runtime error except one that keeps on bugging out. I have all the required functions and have cleaned and deleted the derived data but it still saying that my class does not conform to '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?){

}
}`

Here is the code for the protocol `

// 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)
}`
2

There are 2 best solutions below

3
On
  1. Remove all the functions from the extension

     extension Cashier: MCSessionDelegate.
    
  2. You'll see the error missing subs, click on fix option and it'll add all the required func automatically
  3. if above 2 steps doesn't work , try cleaning the build folder and restart Xcode
0
On

One of your delegate methods is wrong. at localURL: URL should be 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