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

标签 ios swift xcode swift5

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

    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 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 init 方法的末尾执行此操作。

现在问题是随机排序的,您可以使用已经必须跟踪下一个问题的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 
    }
}

关于ios - 我怎样才能让这个功能停止重复问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59015682/

相关文章:

json - swift2中如何从json中读取数据

swift - 扩展 NSDecimalNumber 以符合 ExpressibleByStringLiteral 会导致 LLDB RPC 服务器崩溃

objective-c - Swift 类在 Objective-C .h 文件中不可见

iOS 为库编写单元测试

ios - Swift - 如何更改 UITabBarItem 角标(Badge)字体?

每次用户进入后台至少 30 秒时,在 iOS 13 上运行的 iOS 应用程序都会重置

iphone - 为什么 iOS 模板没有出现在 Xcode 中?

ios - ReactiveCocoa 中冷热信号的例子有哪些?

ios - swift 位置 : Fire every X Meters a Notification

xcode - 线程 1 : Stopped at breakpoint 1