ios - 更新到 Swift 2 后,Game Center 不接受我的代码

标签 ios swift2 game-center

这是我的代码:

if GKLocalPlayer.localPlayer().authenticated {
            if mode == 60 {
                var scoreReporter2 = GKScore(leaderboardIdentifier: "countrymaster60") //leaderboard id here

                scoreReporter2.value = Int64(score) //score variable here (same as above)
                var scoreArray: [GKScore] = [scoreReporter2]
                GKScore.reportScores(scoreArray, withCompletionHandler: {(error : NSError!) -> Void in
                    if error != nil {
                        print("error")
                    }

它给出的错误信息:

Cannot invoke 'reportScores' with an argument list of type '([GKScore], withCompletionHandler: (NSError!) -> Void)'

有什么建议吗?

最佳答案

文档说方法签名是:

class func reportScores(_ scores: [GKScore], withCompletionHandler completionHandler: ((NSError?) -> Void)?)

因此您需要将完成处理程序中的 error 变量设为可选。

改变

GKScore.reportScores(scoreArray, withCompletionHandler: {(error : NSError!) -> Void in 

对于

GKScore.reportScores(scoreArray, withCompletionHandler: {(error : NSError?) -> Void in

关于ios - 更新到 Swift 2 后,Game Center 不接受我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906413/

相关文章:

ios - JSONSerialization.jsonObject

ios - Xcode - 以 64 位编译应用程序时发生错误

objective-c - 检索城市信息(酒店、餐厅、景点等)

ios - UISwitch 没有关闭?

ios - 如何跳过游戏中心排行榜中的语言选择?

ios - 游戏中心成就未重置

ios - 在 GKMatchmakerviewcontroller 中处理取消

iphone - 没有互联网时的 NSURLConnection 行为

ios - 在类之间存储和传递数据的正确方法

swift - 无法将 Swift 嵌套类作为 AnyClass 参数传递