问:如何更新布尔值?

时间:2015-03-13 05:07:41

标签: swift parse-platform

我遇到了一个小问题。我想在我的测验应用中更新Bool值。如果初始值为true并且在用户猜对问题之后,我希望此值变为false。到目前为止,println(“将被更新为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!)")
            }
        }

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:2)

首先,编辑对象,然后将它们保存为解析。试试这个:

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!)")
    }
}