swift - Q : How to Update Boolean Value?

标签 swift parse-platform

我遇到了一个小问题。我正在尝试更新我的 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/

相关文章:

ios - 哪些约束相互冲突?

javascript - 无法获取要插入 JavaScript 字符串的参数值

javascript - 使用 Parse Javascript SDK 显示来自网络返回的嵌套数据

ios - 不从缓存中获取对象 - 解析

ios - 如何检查 swift 打开 URL 的能力?

swift - 如何更改 UIBarButtonItem 的字体样式和大小?

ios - 传入要与对象映射器一起使用的类类型

ios - 类型 'string' 的值没有成员 'containsString'

安卓代码混淆。我想隐藏 Parse.com 的 key

javascript - parse.com 云代码不接受 string.includes() 吗?