我遇到了一个小问题。我正在尝试更新我的 Quiz 应用程序中的 Bool 值。如果初始值为 true 并且在用户猜对了问题之后,我希望这个值变为 false。到目前为止,println("Will be updated to false") 确实向我展示了我想要的结果,但我想不出在 Parse 中更新“check”列中的值的方法。
var query = PFQuery(className:"Questions")
query.whereKey("check", equalTo:true)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
println("Found the true value")
if object["check"] as Bool == true {
println("Will be updated to false")
}
}
}
} else {
// Log details of the failure
println("Error: \(error) \(error.userInfo!)")
}
}
非常感谢您的帮助!
最佳答案
首先,您编辑对象,然后保存它们以供解析。试试这个:
var query = PFQuery(className:"Questions")
query.whereKey("check", equalTo:true)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
// === cache the objects, you want to edit ===
var tmp = [PFObject]()
// ===========================================
for object in objects {
println(object.objectId)
println("Found the true value")
if object["check"] as Bool == true {
println("Will be updated to false")
//=== Set the object to false =======
object["check"] = false
tmp.append(object)
//===================================
}
}
// === save all edited objects ===========
PFObject.saveAllInBackground(tmp, block: {
(success: Bool!, error: NSError!) -> Void in
if success == true {
println("done")
}
})
// =======================================
}
} else {
// Log details of the failure
println("Error: \(error) \(error.userInfo!)")
}
}
关于swift - Q : How to Update Boolean Value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025235/