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:
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:
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.