我在Swift中设置GKMatchmakerViewControllerDelegate时遇到错误吗?

时间:2015-02-14 14:49:17

标签: ios swift game-center gkmatchmaker

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

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`. 

为什么会这样?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

批准答案中的第二项功能已在最新的SDK中更新:

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