Saving Game Center Record in Swift 2
I recently upgraded to fast 2 after downloading Xcode 7 beta and I encountered 2 errors which I fixed with product> clean. I am still stuck with errors related to Game Center.Below is my code to save the highscore. (If it helps, this code is present on two view controllers, with a difference in leader ids and rating variables)
func saveHighscore(score:Int) {
//check if user is signed in
if GKLocalPlayer.localPlayer().authenticated {
var scoreReporter = GKScore(leaderboardIdentifier: "ChineseWeather") //leaderboard id here
scoreReporter.value = Int64(Score) //score variable here (same as above)
var scoreArray: [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: {(error : NSError!) -> Void in
if error != nil {
print("error")
}
})
}
}
On the line where it starts with GKScore, I get the following error:
Cannot call 'reportScores' using argument list like '([GKScore], withCompletionHandler: (NSError!) → Void)'
So I tried to fix it by adding scores: in front of scoreArray like this:
GKScore.reportScores(scores: scoreArray, withCompletionHandler: {(error : NSError!) -> Void in
And it gives me the following error:
Missing argument for parameter 'withEligibleChallenges' when called
Help would be greatly appreciated and well in advance
source to share
According to the preliminary documentation , the method signature has changed like this:
class func reportScores(_ scores: [GKScore],
withCompletionHandler completionHandler: ((NSError?) -> Void)?)
This differs from the old documentation which states:
class func reportScores(_ scores: [AnyObject]!,
withCompletionHandler completionHandler: ((NSError!) -> Void)!)
Note the change to the optional NSError parameter, and also that the entire handler is optional.
So, you will need to change your code so that explicit is error: NSError!
not selected as a completion block parameter.
source to share