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

Game Center doesn't accept my code after I updated to Swift 2

这是我的代码:

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