1
votes

Im trying this tutorial and because of the new swift version some errors came up, one of them was MPCManager does not conform with the protocol MCNearbyServiceAdvertiserDelegate.

I tried to fix it so i changed the header of func advertiser to this one:

func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, invitationHandler: (Bool, MCSession)->Void) {

but then it throws me an error

Cannot assign value '(Bool, MCSession) -> Void' to type '(Bool, MCSession!) -> Void!'

which i don't know how to fix, help appreciated!

The actual file:

    import UIKit
    import MultipeerConnectivity

    protocol MPCManagerDelegate {
        func foundPeer()

        func lostPeer()

        func invitationWasReceived(fromPeer: String)

        func connectedWithPeer(peerID: MCPeerID)
    }

    class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
        var session: MCSession!
        var peer: MCPeerID!
        var browser: MCNearbyServiceBrowser!
        var advertiser: MCNearbyServiceAdvertiser!

        var foundPeers = [MCPeerID]()
        var invitationHandler: (Bool, MCSession!)->Void!
        var delegate: MPCManagerDelegate?

        override init() {
            super.init()

            invitationHandler(false, nil)

            peer = MCPeerID(displayName: UIDevice.currentDevice().name)

            session = MCSession(peer: peer)
            session.delegate = self

            browser = MCNearbyServiceBrowser(peer: peer, serviceType: "appcoda-mpc")
            browser.delegate = self

            advertiser = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: nil, serviceType: "appcoda-mpc")
            advertiser.delegate = self
        }

        func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
            foundPeers.append(peerID)

            delegate?.foundPeer()
        }

        func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
            for (index, aPeer) in EnumerateSequence(foundPeers){
                if aPeer == peerID {
                    foundPeers.removeAtIndex(index)
                    break
                }
            }

            delegate?.lostPeer()
        }

        func browser(browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: NSError) {
            print(error.localizedDescription)
        }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ THE Error occurs here: ~~~~~~~~~~~~~~~~~~~~~~
        func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, invitationHandler: (Bool, MCSession)->Void) {
            self.invitationHandler = invitationHandler

            delegate?.invitationWasReceived(peerID.displayName)
        }

        func advertiser(advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: NSError) {
            print(error.localizedDescription)
        }



        func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState) {
            switch state{
            case MCSessionState.Connected:
                print("Connected to session: \(session)")
                delegate?.connectedWithPeer(peerID)

            case MCSessionState.Connecting:
                print("Connecting to session: \(session)")

            default:
                print("Did not connect to session: \(session)")
            }
        }

        func sendData(dictionaryWithData dictionary: Dictionary<String, String>, toPeer targetPeer: MCPeerID) -> Bool {
            let dataToSend = NSKeyedArchiver.archivedDataWithRootObject(dictionary)
            let peersArray = NSArray(object: targetPeer) as! [MCPeerID]

            do {
                _ = try session.sendData(dataToSend, toPeers: peersArray, withMode: MCSessionSendDataMode.Reliable)
            } catch let error as NSError {
                print(error.localizedDescription)
                return false
            }

            return true
        }

        func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) {
            let dictionary: [String: AnyObject] = ["data": data, "fromPeer": peerID]
            NSNotificationCenter.defaultCenter().postNotificationName("receivedMPCDataNotification", object: dictionary)
        }

        func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress) { }

        func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) { }

        func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) { }

    }
2
Can you please put the snippets of the problematic code into your question? - 0x416e746f6e
Edited! What about now? - Dror Chen
Were you able to fix this issue? - Francis F

2 Answers

0
votes

You defined the invitationHandler variable with (Bool, MCSession!)->Void! type, then assign it with a value (Bool, MCSession)->Void) type. This is the reason which causes your error message.

To fix it, define your advertiser method with the param match type with the invitationHandler variable.

0
votes

The property invitationHandler of the class MPCManager is defined as:

var invitationHandler: (Bool, MCSession!) -> Void!

... while the argument invitationHandler of advertiser method is defined as

invitationHandler: (Bool, MCSession) -> Void

There are 2 mismatches here:

  • Property's return value is Void! while argument returns Void
  • Second argument of the property is MCSession! while method takes MCSession

Firstly, I do not see the point to have Void! as return value. Void means nothing is being returned at all, so no need to implicitly unwrap it.

Secondly, the argument's types must match. So, either both should be MCSession or both MCSession!.

E.g. the following does not produce the error you mention:

var invitationHandler: (Bool, MCSession!) -> Void

// ...

func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, 
    invitationHandler: (Bool, MCSession!) -> Void) {