Hey everyone really struggling with this one, I have a quiz app however I need the questions to not repeat. My var is:
And then my index:
Any help would be greatly appreciated, cheers!
Code Block var gameModels = [Question]()
And then my index:
Code Block if index < (gameModels.count + 1) { //next question print("\(gameModels.shuffle())") let nextQuestion = gameModels[index+1] currentQuestion = nil configureUI(question: nextQuestion) self.scoreBoard += 1 self.scoreLabel.text = String(scoreBoard) questionsAsked += 1 }
Any help would be greatly appreciated, cheers!
Thanks for showing additional context. Generally it is better to include whole method at least, preferably whole struct or whole class.
It is not clear enough, for example, how nextQuestion and currentQuestion are used? How do you get the value of index?
All such things may affect.
But I will do my best guess to write an answer under the currently shown info.
First set up unusedQuestions where you initialize gameModels:
And use unusedQuestions like this:
If you find something difficult to apply my sample code to your project, please tell me with detailed info about it.
It is not clear enough, for example, how nextQuestion and currentQuestion are used? How do you get the value of index?
All such things may affect.
But I will do my best guess to write an answer under the currently shown info.
First set up unusedQuestions where you initialize gameModels:
Code Block var unusedQuestions: [Question] = [] private func setupQuestions() { gameModels.append(Question(text: "How old am I", answers: [ Answer(text: "18", correct: false), Answer(text: "21", correct: true), Answer(text: "20", correct: false), Answer(text: "None Of The Above", correct: false) ])) gameModels.append(Question(text: "Where am I from?", answers: [ Answer(text: "NZ", correct: true), Answer(text: "Australia", correct: false), Answer(text: "France", correct: false), Answer(text: "US", correct: false) ])) unusedQuestions = gameModels }
And use unusedQuestions like this:
Code Block if let index = unusedQuestions.indices.randomElement() { let nextQuestion = unusedQuestions[index] unusedQuestions.remove(at: index) currentQuestion = nil configureUI(question: nextQuestion) self.scoreBoard += 1 self.scoreLabel.text = String(scoreBoard) //questionsAsked += 1 //<- You can calculate `questionsAsked` with `gameModels.count - unusedQuestions.count` } else { print("No more unused questions") }
If you find something difficult to apply my sample code to your project, please tell me with detailed info about it.