Im making a Quiz game with swift and I have run into an issue. My RandomQuestionGenerator function doesn't seem to be changing my correctAnswer variable. I tested this by changing the if correctAnswer == 1 to if correctAnswer == 0 and the code when I press the button runs. Im guessing there is a stupid obvious solution but I can't figure it out.
var correctAnswer = 0
func RandomQuestionGenerator(){
var randomQuestion =
Int(arc4random_uniform(UInt32(fullQuestions.count)))
var currentQuestion = fullQuestions[randomQuestion]
var correctAnswer = currentQuestion.answer
Label.text = currentQuestion.Question
Button1.setTitle(currentQuestion.answers[0], for: .normal)
Button2.setTitle(currentQuestion.answers[1], for: .normal)
Button3.setTitle(currentQuestion.answers[2], for: .normal)
Button4.setTitle(currentQuestion.answers[3], for: .normal)
fullQuestions.remove(at: randomQuestion)
}
override func viewDidAppear(_ animated: Bool) {
appendQuestions()
RandomQuestionGenerator()
}
@IBAction func Button1Pressed(_ sender: Any) {
if correctAnswer == 1{
RandomQuestionGenerator()
}
}
var currentQuestion = fullQuestions[randomQuestion]is not a simple assignment. It's a variable declaration in the local code-block. Newly declared variable shadows the same names in the outer scope. Just removevarin that line. You should always distinguish variable declarations and assignments.