3000 3000 - 2 months ago 28
iOS Question

Conversion to Swift 3 issue

I was following this good tutorial and I downloaded the related project: https://www.ralfebert.de/tutorials/ios-swift-multipeer-connectivity/

Then, I opened it in XCode and converted it to Swift 3.

In the code, there was this:

func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!) {
NSLog("%@", "foundPeer: \(peerID)")
NSLog("%@", "invitePeer: \(peerID)")
browser.invitePeer(peerID, toSession: self.session, withContext: nil, timeout: 10)
}


and XCode converted it to this:

func browser(_: MCNearbyServiceBrowser, foundPeer: MCPeerID, withDiscoveryInfo: [String : String]?){
//...
browser.invitePeer(MCPeerID, to: self.session, withContext: nil, timeout: 10)
}


As you can see, the first argument is no more
browser
: this causes the fact that the method can't call
invitePeer
(at that point nobody knows what
browser
is).

How can I convert my code to call invitePeer and respect the new signature?

Answer

Please change like below , Xcode migration will add _ before method first argument

  func browser(_ browser: MCNearbyServiceBrowser, foundPeer: MCPeerID, withDiscoveryInfo: [String : String]?){
      //...
      browser.invitePeer(MCPeerID, to: self.session, withContext: nil, timeout: 10)
    }