【问题标题】:Calling random objectId from Parse从 Parse 调用随机 objectId
【发布时间】:2014-11-24 04:43:00
【问题描述】:

在我的类 VoteCount 的 Parse 数据控制台上,我有一个名为 objectId 的字符串数据垂直列,并且该类中的每个水平数据行都有一个唯一的 objectId。正如您在此处看到的,我正在从 objectId BiEM17uUYT 检索数据:

    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("BiEM17uUYT") {
        (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)"

        }

我只是不知道如何从随机 objectId 调用数据。我只能从特定的 objectId 调用数据,在本例中是 BiEM17uUYT。如何从整列中的随机 objectId 收集所有数据,而不仅仅是 BiEM17uUYT?这将允许我完全从 Parse 更新我的应用数据,而不必不断提交全新的更新。

注意:与“votes”、“votes2”、“optionName”和“optionName2”一样,“objectId”是另一列数据。

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    简答:在您的班级中,创建一个包含数字的变量,并递增该数字,使其始终是唯一且连续的。然后调用 Parse 以获取对象的总数并运行 RAND 以获取介于 0 和您的 Max 数之间的数字。然后在右侧字段中查询具有该编号的对象。

    更长的答案:试试这样的:

    var query = PFQuery(className: "VoteCount")
    query.countObjectsInBackgroundWithBlock {
      (count: Int, 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)"
    
        }
      }
    }
    

    【讨论】:

    • 现在,我在 query.countObjectsInBackgroundWithBlock 上收到一个错误,指出“Int32 不是 Int 的子类型”。我尝试将 count: Int 更改为 count: Int32 但这也不起作用。知道这是为什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2013-07-10
    • 1970-01-01
    相关资源
    最近更新 更多