【问题标题】:Receive, edit and update a value in Parse.com在 Parse.com 中接收、编辑和更新值
【发布时间】:2015-10-19 21:41:55
【问题描述】:

我正在尝试更新解析中的值,这是我接收该值的代码:

var query = PFQuery(className:"currentUploads")
            query.whereKey("objectId", equalTo:post.objID)
            query.findObjectsInBackgroundWithBlock {
                (restaurants: [AnyObject]?, error: NSError?) -> Void in
                if error == nil {
                    if let objects = restaurants as? [PFObject] {
                        var firstObject = objects[0]
                        dispatch_async(dispatch_get_main_queue(),{
                            self.currentVotes = firstObject["reportedCount"] as! String
                            println(self.currentVotes)
                        })

                    }
                }
            }

在输出中,它给了我来自println 的数字 5,这是正确的。但现在我想将数字增加 1,使其变为 6,并在 parse.com 中更新它

我该怎么做?

【问题讨论】:

  • 添加一个并保存.....
  • @ChrisL 这可以通过使用PFObject(withoutDataWithClassName: objectId:)然后调用incrementKey来更有效地完成

标签: xcode swift parse-platform


【解决方案1】:

由于您有 objectId,因此您无需先使用指针进行查询即可执行此操作。

试试下面的

// Create a pointer to an object of class currentUploads with post.objID
let currentUpload = PFObject(withoutDataWithClassName: "currentUploads", objectId: post.objID)

// Increment the key
currentUpload.incrementKey("reportedCount")    

// Save
currentUpload.saveInBackgroundWithBlock {
    (success: Bool, error: NSError?) -> Void in
    if (success) {
        // The object has been incremented
    } else {
        // There was a problem, check error.description
    }
}

【讨论】:

  • 谢谢!我也让它工作了,但以我的方式我不得不使用 2066 个字符和 36 行代码 :) 无论如何,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多