swiftgame-center-leaderboard

Swift: Retrieve LocalPlayer GC Score


Below is my code for getting localplayer's game center score.

if (GKLocalPlayer.localPlayer().authenticated) {
    GKLeaderboard.loadLeaderboardsWithCompletionHandler { objects, error in
        if let e = error {
            print(e)
        } else {
            let leaderboards = objects! as [GKLeaderboard]
            print(leaderboards)
            for leaderboard in leaderboards {
                print(leaderboard)
                print(leaderboard.localPlayerScore) // This prints nil
            }
        }
    }
}

I get nil for print(leaderboard.localPlayerScore) ,While I know for sure that local player has score saved on this leaderboard. Am I missing something?


Solution

  • You got to call

    leaderboard.loadScoresWithCompletionHandler() { scores, error in
        if error == nil {
              print(leaderboard.localPlayerScore)
        }
    }