在Parse.com中接收,编辑和更新值

时间:2015-10-19 21:41:56

标签: xcode swift parse-platform

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

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中更新

我该怎么做?

1 个答案:

答案 0 :(得分: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
    }
}