我怎样才能让这个功能停止重复问题?

How would I get this function to stop repeating questions?

我正在制作一个测验应用程序,我有一个不断重复问题的功能,这是功能:

    func updateQuestion(){
        if questionNumber < allQuestions.list.count{
        allQuestions.list.shuffle()
        questionsView.text = allQuestions.list[questionNumber].question
        optionA.setTitle(allQuestions.list[questionNumber].optionA, for: UIControl.State.normal)
        optionB.setTitle(allQuestions.list[questionNumber].optionB, for: UIControl.State.normal)
        optionC.setTitle(allQuestions.list[questionNumber].optionC, for: UIControl.State.normal)
        optionD.setTitle(allQuestions.list[questionNumber].optionD, for: UIControl.State.normal)
        selectedAnswer = allQuestions.list[questionNumber].correctAnswer

   }

我的问题存储在另一个名为 QuestionsBank 的 class 中,我怎样才能让它们不再重复?

class QuestionBank {
    var list = [Question]()

    init() {

        list.append(Question(questionText: "What is his middle name?", choiceA: "E***k", choiceB: "E******o", choiceC: "G******s", choiceD: "He doesn't have a middle name", answer: 2))

        list.append(Question(questionText: "What is the name of his first dog?", choiceA: "Neka", choiceB: "Sadie", choiceC: "Bear", choiceD: "Jeffery", answer: 3))

        list.append(Question(questionText: "What is his energy drink of choice?", choiceA: "Monster", choiceB: "Reign", choiceC: "Rockstar", choiceD: "Bang", answer: 2))
    }
}

我觉得我需要以某种方式为每个问题分配标签以识别它们,但我不知道该怎么做。我是编程新手,所以任何帮助将不胜感激,尽管我可能会问很多问题。

您需要做的就是不要在 updateQuestion 函数中打乱数组,而是在 QuestionBank 初始化方法的末尾执行此操作。

现在问题是随机排序的,你使用 questionNumber 你已经必须跟踪下一个问题。

另一个类似的解决方案是让 QuestionBank 负责处理问题

class QuestionBank {
    private let list = [Question]()
    private var index: Int

    init() {
        var temp = [Question(questionText: "What is his middle name?", choiceA: "E***k", choiceB: "E******o", choiceC: "G******s", choiceD: "He doesn't have a middle name", answer: 2),
            Question(questionText: "What is the name of his first dog?", choiceA: "Neka", choiceB: "Sadie", choiceC: "Bear", choiceD: "Jeffery", answer: 3),
            Question(questionText: "What is his energy drink of choice?", choiceA: "Monster", choiceB: "Reign", choiceC: "Rockstar", choiceD: "Bang", answer: 2)]
        index = 0
        list = temp.shuffled()
    }

    func nextQuestion() -> Question? {
        guard index < list.count else { return nil }
        let question = list[index]
        index += 1
        return question
    }
}

然后更新函数将是

func updateQuestion(){
    if let question = allQuestions.nextQuestion() {
        questionsView.text = question.questionText
        optionA.setTitle(question.optionA, for: UIControl.State.normal)
        optionB.setTitle(question.optionB, for: UIControl.State.normal)
        optionC.setTitle(question.optionC, for: UIControl.State.normal)
        optionD.setTitle(question.optionD, for: UIControl.State.normal)
        selectedAnswer = question.correctAnswer
    } else {
         // No more questions to ask, handle this here 
    }
}