无法使用不同的Parse对象ID重复查询

时间:2014-11-22 05:59:31

标签: ios swift

我有一个查询需要访问Parse数据控制器上某一行的数据。每行数据都有不同的对象ID,似乎我需要为每一行数据编写一个全新的函数。例如,这里的功能是:

    @IBAction func addVote1(sender: AnyObject) {
    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("BiEM17uUYT") {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount1.incrementKey("votes")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
            let votes = voteCount1["votes"] as Int
            let votes2 = voteCount1["votes2"] as Int
            self.pollResults1.text = "\(votes)"
        }

        }
    }

问题是,我有另一个查询,该按钮可以调用完全相同的函数,并且仅在" getObjectInBackgroundWithId"中使用不同的ID进行查询。如下:

    @IBAction func addVote1(sender: AnyObject) {
    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("TtKGatVCi9") {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount1.incrementKey("votes")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
            let votes = voteCount1["votes"] as Int
            let votes2 = voteCount1["votes2"] as Int
            self.pollResults1.text = "\(votes)"
        }

        }
    }

如何制作它以便我只需要编写一次代码并让它使用随机对象ID访问数据?到目前为止,我在viewDidLoad方法中尝试了这个,但只收到了错误消息:

        let array = ["BiEM17uUYT", "TtKGatVCi9"]
    let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
    var objectId = array[randomIndex]

    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("objectId") {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount1.incrementKey("votes")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
            let votes = voteCount1["votes"] as Int
            let votes2 = voteCount1["votes2"] as Int
            self.pollResults1.text = "\(votes)"
        }
    }

我做错了什么,我如何才能访问数组中的随机objectId?

1 个答案:

答案 0 :(得分:0)

您正在传递一个值为" objectId"的字符串。到getObjectInBackgroundWithId而不是objectId的值。

变化:

query.getObjectInBackgroundWithId("objectId") {

为:

query.getObjectInBackgroundWithId(objectId) {