当我在 Swift 中设置 GKMatchmakerViewControllerDelegate 时出现错误?

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

我想为我的游戏设置配对viewController。为此,我将 GKMatchmakerViewControllerDelegate 委托添加到名为 Home 的主 UIViewController 中,这样它看起来像:

class Home: UIViewController, GKGameCenterControllerDelegate, GKMatchmakerViewControllerDelegate {

要加载配对界面,我正在使用此代码:

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

不过,当我尝试 运行 代码时,接下来 class Home: ... 收到以下错误。错误消息说:

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

为什么会这样?

大多数情况下,您遇到该错误是因为您没有实现特定协议所需的功能。您可以通过命令单击该协议来查看它们。添加以下方法:

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

批准答案中的第二个函数已更新到最新的 SDK 中:

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