Tom*_*Fox 0 random arc4random ios firebase swift
我arc4random
用来生成10个随机数,这样我就可以查询firebase来获取包含随机生成数字的问题.问题是我不希望任何数字出现多次,因此没有重复的问题.目前的代码如下......
import UIKit
import Firebase
class QuestionViewController: UIViewController {
var amountOfQuestions: UInt32 = 40
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// 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: "https://test.firebaseio.com/questions")
// 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)
})
}
}
@IBAction func truepressed(sender: AnyObject) {
}
@IBAction func falsePressed(sender: AnyObject) {
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用一个数组来存储您想要随机的值,在您的情况下,[1,2,3 .... 10],然后使用arc4random获取内部任何值的随机索引(0..9 ),获取值并从数组中删除它.然后你永远不会从数组中得到相同的数字.
归档时间: |
|
查看次数: |
521 次 |
最近记录: |