Do not repeat question, removeAtIndex

Hey everyone really struggling with this one, I have a quiz app however I need the questions to not repeat. My var is:

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!
Answered by OOPer in 668008022
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:
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.
You should better add another variable unusedQuestions.

When you pick a new question from unusedQuestions, remove it.
Or you can remove the question from gameModels.

Another way would be to add a property to Question struct:

Code Block
struct Question {
    // What you had so far
    var answered = false
}

Then you could show the answered questions as dimmed text. When selecting a question, toggle answered to true. And add a test to check if question is answered.

Please, don't forget to close the thread once done.
Could you please expand?

Could you please expand?

Please clarify what you mean by expand?

If you could show more context about your code, I would have shown some concrete code.
Apologies, theres my code above, my questions are located in

Code Block
   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)
    ]))
}

And at the bottom:

Code Block
struct Question {
  let text: String
  let answers: [Answer]
}
struct Answer {
  let text: String
  let correct: Bool //true / false
}

Forgot to mention if it worthwhile knowing that my first block of code I showed you that goes onto the next question is:

Code Block
     let answer = question.answers[indexPath.row]
    if checkAnswer(answer: answer, question: question) {

Thanks for all the help so far by the way :)
Accepted Answer
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:
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.
Thanks mate, greatly appreciated!
Do not repeat question, removeAtIndex
 
 
Q