I am creating a simple quiz app. There are four choices and the placement of choices is generated randomly. Four UILabels are set to display choices.
I have written the following code and it usually works well but sometimes the same choices are displayed.
ex) 2017 2015 2015 1700
The error should be the for loops but I can't figure out.
let questions = ["What year is now?"]
let answers = [["2017", "2015", "1000", "1700"]]
var rightAnswerPlacement:UInt32 = 0
var currentQuestion = 0
//Function that displays new question
func newQuestion()
{
lbl.text = questions[currentQuestion]
rightAnswerPlacement = arc4random_uniform(4)+1
// Create a button
var button:UIButton = UIButton()
var x = 1
for i in 1...4
{
//Create a button
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement))
{
button.setTitle(answers[currentQuestion][0], for: .normal)
}
else
{
button.setTitle(answers[currentQuestion][x], for: .normal)
x = 2
}
}
button.setTitle(answers[currentQuestion][3], for: .normal)
currentQuestion += 1
}