简体   繁体   中英

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?

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!) {
}

The second function in the approved answer was updated in latest SDK:

func matchmakerViewController(viewController: GKMatchmakerViewController, didFindHostedPlayers players: [GKPlayer]) {
   ... 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM