iosswiftgame-centergkmatchmaker

While am I getting an error while setting up GKMatchmakerViewControllerDelegate in Swift?


I would like like to set up the matchmaking viewController of my game. To do so, I add the GKMatchmakerViewControllerDelegate delegate to my main UIViewController called Home, so that it looks like:

class Home: UIViewController, GKGameCenterControllerDelegate, GKMatchmakerViewControllerDelegate {

To load up the matchmaking interface I am using this code:

func openMatchmaker() {
    var gcViewController: GKMatchmakerViewController = GKMatchmakerViewController(rootViewController: self)

    gcViewController.matchmakerDelegate = self

    gcViewController.hosted = false
    gcViewController.matchRequest.minPlayers = 2
    gcViewController.matchRequest.maxPlayers = 2
    gcViewController.matchRequest.defaultNumberOfPlayers = 2

    self.showViewController(gcViewController, sender: self)
    self.navigationController?.pushViewController(gcViewController, animated: true)
}

Though, I receive the following error next class Home: ... to when I try to run the code. The error message says:

 Type 'Home' does not conform to protocol `GKMatchmakerViewControllerDelegate`. 

Why is that happening?


Solution

  • Most of the time when you get that error is because you have not implemented the required functions of the particular protocol. You can see them by command clicking on that protocol. Add the following methods:

    func matchmakerViewController(viewController: GKMatchmakerViewController!,
        didFailWithError error: NSError!) {
    }
    
    func matchmakerViewController(viewController: GKMatchmakerViewController!,
        didFindHostedPlayers players: [AnyObject]!) {
    }
    
    func matchmakerViewControllerWasCancelled(viewController: GKMatchmakerViewController!) {
    }
    
    func matchmakerViewController(viewController: GKMatchmakerViewController!,
        hostedPlayerDidAccept player: GKPlayer!) {
    }