gpt4 book ai didi

ios - GKLeaderboard.localPlayerScore 在 iOS 8.1.3 上返回 nil?

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:53 30 4
gpt4 key购买 nike

我一直在使用 GKLeaderboard.loadScoresWithCompletionHandler 然后是 localPlayerScore 属性,这样我就可以从 Game Center 检索用户的高分,当我游戏启动。

现在在 iOS 7.08.1.2 上,当我访问 localPlayerScore 属性时,我得到一个有效的 GKScore 对象。但是,在 iOS 8.1.3 上,我得到的结果为 nil,这会导致我的应用程序崩溃。我收到一个运行时错误,说它在解包可选时发现 nil。

这是与该问题相关的代码片段:

func compareLocalHighScoreFromLeaderboards()
{
// This is to fetch data from the high score leaderboard
let leaderboardRequest = GKLeaderboard()
leaderboardRequest.identifier = GlobalVariables.sharedInstance._highScoreLeaderboardID
leaderboardRequest.loadScoresWithCompletionHandler { (scores, error) -> Void in

if error != nil
{
println("Error fetching score from leaderboards: \(error))")
}
else if scores != nil
{
println("entered loading scores from leaderboards")


let leaderboardScore = leaderboardRequest.localPlayerScore // this returns a GKScore object

var playerLocalHighScore = NSUserDefaults.standardUserDefaults().objectForKey(GlobalVariables.sharedInstance._highScoreKey) as NSNumber


// Check first if the saved highscore is updated.
if playerLocalHighScore.longLongValue != leaderboardScore.value
{
// this means that we don't have the updated leaderboard score in our device
if playerLocalHighScore.longLongValue > leaderboardScore.value
{

println("Local score is greater than leaderboard highscore. Do nothing because GameKit will automatically report to GC when there is internet connectivity")
}
else if playerLocalHighScore.longLongValue < leaderboardScore.value
{

// update the local highscore with the leaderboard highscore
let updatedHighscore: NSNumber = NSNumber(longLong: leaderboardScore.value)
NSUserDefaults.standardUserDefaults().setObject(updatedHighscore, forKey: GlobalVariables.sharedInstance._highScoreKey)

// send notification message to MainMenuScene to update the SKLabelNode
NSNotificationCenter.defaultCenter().postNotificationName(UpdatedLocalHighScore, object: nil)
}
}
else
{
println("The local highscore and the leaderboard highscore are already in sync")
}


}
}
}

我一直在为我以前的游戏(顺便说一下,这些游戏已获批准并在 Appstore 上线)使用相同的代码来检索用户的游戏中心高分,直到 8.1.3.

我当前的应用程序被 Apple 拒绝,因为他们使用 iOS 8.1.3 设备对其进行了测试,但它崩溃了。我做了一些调试,发现这个 localPlayerScore 为 nil 是问题的根源。

有人最近遇到过这个问题吗?有谁知道如何解决这个问题?

感谢任何帮助。

最佳答案

更改 else if scores != nil为此else if leaderBoardRequest.localPlayerScore != nil ,因为 scores包含leaderboardRequest.identifier的所有信息当用户第一次打开你的游戏时,leaderBoardRequest.localPlayerScore是空的,但是 scores不一定

   let leaderboardRequest = GKLeaderboard()
leaderboardRequest.identifier = GlobalVariables.sharedInstance._highScoreLeaderboardID
leaderboardRequest.loadScoresWithCompletionHandler { (scores, error) -> Void in
if error != nil
{
println("Error fetching score from leaderboards: \(error))")
}
else if leaderBoardRequest.localPlayerScore != nil
{
println("entered loading scores from leaderboards")


let leaderboardScore = leaderboardRequest.localPlayerScore // this returns a GKScore object

var playerLocalHighScore = NSUserDefaults.standardUserDefaults().objectForKey(GlobalVariables.sharedInstance._highScoreKey) as NSNumber


// Check first if the saved highscore is updated.
if playerLocalHighScore.longLongValue != leaderboardScore.value
{
// this means that we don't have the updated leaderboard score in our device
if playerLocalHighScore.longLongValue > leaderboardScore.value
{

println("Local score is greater than leaderboard highscore. Do nothing because GameKit will automatically report to GC when there is internet connectivity")
}
else if playerLocalHighScore.longLongValue < leaderboardScore.value
{

// update the local highscore with the leaderboard highscore
let updatedHighscore: NSNumber = NSNumber(longLong: leaderboardScore.value)
NSUserDefaults.standardUserDefaults().setObject(updatedHighscore, forKey: GlobalVariables.sharedInstance._highScoreKey)

// send notification message to MainMenuScene to update the SKLabelNode
NSNotificationCenter.defaultCenter().postNotificationName(UpdatedLocalHighScore, object: nil)
}
}
else
{
println("The local highscore and the leaderboard highscore are already in sync")
}


}else{
println("leaderBoardRequest.localPlayerScore is empty")}
///here report the local high score to leaderboard
}

关于ios - GKLeaderboard.localPlayerScore 在 iOS 8.1.3 上返回 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28519503/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com