从Parse中的随机行调用值?

时间:2014-11-19 05:21:10

标签: ios xcode swift parse-platform

我试图弄清楚如何为随机行中的每个变量调用所有列的值,并且每行在Parse上由其自己的特定objectId(列的名称)表示。这是我在每列中定义变量值的地方:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    var voteCount1 = PFObject(className: "VoteCount")
        voteCount1["choices"] = 2
        voteCount1["votes"] = Int()
        voteCount1["votes2"] = Int()
        voteCount1["optionName"] = String()
        voteCount1["optionName2"] = String()
}

我已经弄清楚如何制作它,以便按钮会对特定行中的变量投票计数(定义为Int)进行投票对象ID,但我不知道如何从随机对象ID调用轮询并将投票发送到该行中的相应选项。这是我向特定对象ID的变量添加投票的IBAction:

@IBAction func addVote1(sender: AnyObject) {
    for button in self.buttons {
        button.enabled = false
    }

    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
        let percent1 = votes * 100 / (votes + votes2)
        let percent2 = votes2 * 100 / (votes + votes2)
        self.pollResults1.text = "\(percent1)% (\(votes))                \(percent2)% (\(votes2))"
        }

    }

如何从随机行调用值?

更新:我刚尝试从Objective-C重新编写以下内容,但我不确定它是否正确:

var query = PFQuery(className: "VoteCount")
        query.findObjectsInBackgroundWithBlock {
            (objects: NSArray, error: NSError!) -> Void in
            if error == nil {
                randNumber = arc4random_uniform(count)
                query2.whereKey("voteNumber", equalTo:randNumber)
                query2.getFirstObjectInBackgroundWithBlock {
                    (voteCount1: PFObject!, error: NSError!) -> Void in
                    if error != nil {
                        NSLog("%@", error)
                    } else {
                        let votes = voteCount1["votes"] as Int
                        let votes2 = voteCount1["votes2"] as Int
                        let option1 = voteCount1["optionName"] as String
                        let option2 = voteCount1["optionName2"] as String
                        self.showOption1.text = "\(option1)"
                        self.showOption2.text = "\(option2)"
            }
                }
            }

1 个答案:

答案 0 :(得分:0)

您永远不会在代码中对对象ID进行硬编码。为了获取随机行,只需从Parse中获取所有行,然后随机选择一行。

例如

PFQuery *query = [PFQuery queryWithClassName:@"VoteCount"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    NSInteger index=arc4random_uniform(objects.count);
    NSLog("The random object is %@",objects[index]);
  } else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
  }
}];

对于更有用的实现,您可能会将objects数组存储到属性中,以便您可以从块外部引用它。

请注意,默认情况下,仅返回前100行。您可以将限制增加到1000.超过1000,您必须使用多个查询的分页。见https://parse.com/questions/fetch-all-data-in-a-table-using-pfquery

var query = PFQuery(className: "VoteCount")
query.findObjectsInBackgroundWithBlock {
        (objects: NSArray, error: NSError!) -> Void in
        if error == nil {
            if let randomObject=objects[arc4random_uniform(objects.count)] {
                 println("The random object is \(randomObject)\")
             }
        } else {
             println("error \(error)")
        }
}