以下是保存用户分数以解析的代码段。除了为用户创建新条目而不是更新用户当前高分的部分之外,它工作正常,这意味着如果John击败他自己的分数10次,我可以有10个条目用于'john doe'。如何进行解析以便解析首先检查用户是否具有高分并更新,并且仅在用户没有当前高分条目时才创建新条目。
// Save Highscore to parse using swift.
let gameScore = PFObject(className:"HighScore")
gameScore.setObject(currentScore, forKey: "highScore")
if let _ = PFUser.currentUser()?.username {
gameScore.setObject((PFUser.currentUser()?["name"])!, forKey: "player")
gameScore.setObject(currentScore, forKey: "highScore")
//gameScore.setObject((PFUser.currentUser()?["image"])!, forKey: "PlayerImage")
gameScore.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// The object has been saved.
print("High Score saved to Parse")
} else {
// There was a problem, check error.description
print("Error in saving High Score to Parse.")
}
}
}
答案 0 :(得分:0)
我在解析文档中找到了一个示例解决方案。
var query = PFQuery(className:"GameScore")
query.getObjectInBackgroundWithId("xWMyZEGZ") {
(gameScore: PFObject?, error: NSError?) -> Void in
if error != nil {
print(error)
} else if let gameScore = gameScore {
gameScore["cheatMode"] = true
gameScore["score"] = 1338
gameScore.saveInBackground()
}
}