0

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)
}`
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • Are you really building apps to be backward compatible to iOS < 7.0 ? If not, try removing the @available(...) and just set your iOS target to >= 7.0. In most cases you should no longer be targeting such old versions of iOS. – ekscrypto Aug 13 '18 at 02:06
  • @ekscrypto thank you for the advice*. I removed the @available(...) and changed my iOS target to 8.0 now it's telling me to add a `didStartReceivingResourceWithName` stub which I already have on my code. – Martin Jaycy Halum Aug 13 '18 at 02:12
  • It should have a “fix” button in the warning bubble. If you click on it check the stub it created, most likely you have one of the type wrong or a parameter name changed – ekscrypto Aug 13 '18 at 02:14
  • I clicked on the fix button and it's showing the exact same functions. Am I missing something? – Martin Jaycy Halum Aug 13 '18 at 02:39

2 Answers2

0
  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
Tushar Katyal
  • 412
  • 5
  • 12
0

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

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160