簡體   English   中英

在Swift中設置GKMatchmakerViewControllerDelegate時出現錯誤嗎?

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

我想設置我的游戲的配對viewController 為此,我將GKMatchmakerViewControllerDelegate委托添加到名為HomeUIViewController ,如下所示:

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]) {
   ... 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM