lundi 22 février 2016

Using arc4random to generate ten random numbers

I am using arc4random to generate 10 random numbers so I can then query firebase to get the questions that contain the randomly generated numbers. The problem is that I don't want any number to appear more than once so then there are not duplicate questions. Current code below...

Thanks in advance, Tom Fox

import UIKit
import Firebase

class QuestionViewController: UIViewController {

var amountOfQuestions: UInt32 = 40

override func viewDidLoad() {
    super.viewDidLoad()

    //Use a for loop to get 10 questions
    for _ in 1...10{
        //generate a random number between 1 and the amount of questions you have
        let randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1
        print(randomNumber)
        //The reference to your questions in firebase (this is an example from firebase itself)
        let ref = Firebase(url: "http://ift.tt/1KF6m2o")
        //Order the questions on their value and get the one that has the random value
        ref.queryOrderedByChild("value").queryEqualToValue(randomNumber)
            .observeEventType(.ChildAdded, withBlock: {
                snapshot in
                //Do something with the question
                print(snapshot.key)
            })
    }
    // Do any additional setup after loading the view.
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

   }



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

@IBAction func truepressed(sender: AnyObject) {
}

@IBAction func falsePressed(sender: AnyObject) {
}

}




Aucun commentaire:

Enregistrer un commentaire