Ios 在Swift中重新编写随机查询

Ios 在Swift中重新编写随机查询,ios,swift,Ios,Swift,我试图重新编写这段代码,从我的解析数据库中检索一行随机数据。以下是Objective-C中的代码: // Get the index for the new object. PFObject *master = [[PFQuery queryWithClassName:@"Master"] getFirstObject]; [master incrementKey:@"nextCardIndex"]; [master save]; int index = [master objectForKey

我试图重新编写这段代码,从我的解析数据库中检索一行随机数据。以下是Objective-C中的代码:

// Get the index for the new object.
PFObject *master = [[PFQuery queryWithClassName:@"Master"] getFirstObject];
[master incrementKey:@"nextCardIndex"];
[master save];
int index = [master objectForKey:@"nextCardIndex"] - 1;

// Create the object itself and assign it the id.
PFObject *card = [PFObject objectWithClassName:@"Card"];
[card setObject:[NSNumber numberWithInt:index] forKey:@"index"];
[card setObject:@"king of hearts" forKey:@"description"];
[card save];

// Get the max index of any card (+1).
PFObject *master = [[PFQuery queryWithClassName:@"Master"] getFirstObject];
int maxIndex = [master objectForKey:@"nextCardIndex"];

// Randomly pick an index in the range of all indices.
int randomIndex = arc4random() % maxIndex;

// Get the card with that particular index.
PFQuery *cardQuery = [PFQuery queryWithClassName:@"Card"];
[cardQuery whereKey:@"index" equalTo:[NSNumber numberWithInt:randomIndex]];
PFObject *card = [cardQuery getFirstObject];
到目前为止,我把它解释为这样写的:

    @IBOutlet var showOption1: UILabel!

@IBOutlet var showOption2: UILabel!

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()
            voteCount1["objectId"] = String()

            var voteCount2 = PFObject(className: "VoteCount2")
            voteCount2["choices"] = 3
            voteCount2["votes"] = Int()
            voteCount2["votes2"] = Int()
            voteCount2["votes3"] = Int()
            voteCount2["optionName"] = String()
            voteCount2["optionName2"] = String()
            voteCount2["optionName3"] = String()

            var voteCount3 = PFObject(className: "VoteCount3")
            voteCount3["choices"] = 4
            voteCount3["votes"] = Int()
            voteCount3["votes2"] = Int()
            voteCount3["votes3"] = Int()
            voteCount3["votes4"] = Int()
            voteCount3["optionName"] = String()
            voteCount3["optionName2"] = String()
            voteCount3["optionName3"] = String()
            voteCount3["optionName4"] = String()


            var query = PFQuery(className: "VoteCount")
            query.countObjectsInBackgroundWithBlock {
                (count: Int32, error: NSError!) -> Void in
                if error == nil {
                    let randNumber = Int(arc4random_uniform(UInt32(count)))
                    query.whereKey("voteNumber", equalTo: randNumber)
                    query.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)"
                        }
                    }
                } else {
                    println("error \(error)")
                }
            }

        }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet weak var pollResults: UILabel!

@IBAction func addVote1(sender: AnyObject) {
    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("voteNumber") {
        (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.pollResults.text = "\(votes)    \(votes2)"
        }
        }
    }

问题是我无法让iAction向objectId中的值添加投票,该值在第一个var查询下的viewDidLoad函数下随机调用。到目前为止我错过了什么?我将其更改为添加一些我自己的类名,即VoteCount。我放对地方了吗?我想做的就是从我的解析数据库中随机调用一行中的所有数据

有什么问题?您是否遇到运行时错误或代码根本不起作用?我已经更新了我的帖子。问题是我不能在iAction中调用与原始var查询中调用的相同的随机值。当我按下按钮时,如何将同一随机检索的号码重新调用到iAction中?