我怎样才能在下面的代码中实现混洗功能?

How would I be able to implement a shuffled function in the below code?

我想洗牌问题。但是,我不确定如何实现这一点。这是下面的代码:

import UIKit

class GameViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var gameModels = [Question]()

    var currentQuestion: Question?

    @IBOutlet var label: UILabel!
    @IBOutlet var table: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        table.delegate = self
        table.dataSource = self
        setupQuestions()
        configureUI(question: gameModels.first!)

    }

    private func configureUI(question: Question) {
        label.text = question.text
        currentQuestion = question
        table.reloadData()

    }

    private func checkAnswer(answer: Answer, question: Question) -> Bool{
        return question.answers.contains(where: { [=10=].text == answer.text }) && answer.correct
    }

    private func setupQuestions(){
        gameModels.append(Question(text: "What is 2 +2", answers: [
            Answer(text: "1", correct: false),
            Answer(text: "2", correct: false),
            Answer(text: "3", correct: false),
            Answer(text: "4", correct: true)

        ]))
        gameModels.append(Question(text: "What is 2 +6", answers: [
            Answer(text: "1", correct: false),
            Answer(text: "2", correct: false),
            Answer(text: "3", correct: false),
            Answer(text: "8", correct: true)

        ]))
        gameModels.append(Question(text: "What is 2 +10", answers: [
            Answer(text: "1", correct: false),
            Answer(text: "2", correct: false),
            Answer(text: "3", correct: false),
            Answer(text: "12", correct: true)

        ]))
        gameModels.append(Question(text: "What is 2+9", answers: [
            Answer(text: "11", correct: true),
            Answer(text: "2", correct: false),
            Answer(text: "3", correct: false),
            Answer(text: "12", correct: false)

        ]))
    }

    // table view functions

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return currentQuestion?.answers.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = currentQuestion?.answers[indexPath.row].text
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        guard let question = currentQuestion else {
            return
        }

        let answer = question.answers[indexPath.row]

        if checkAnswer(answer: answer, question: question) {
            // correct
            if let index = gameModels.firstIndex(where: { [=10=].text == question.text }) {
                if index < (gameModels.count - 1){
                // next question
                    let nextQuestion = gameModels[index + 1]
                    print("\(nextQuestion.text)")
                    currentQuestion = nil
                    configureUI(question: nextQuestion)
            }
            else{
                    // end of game
                    let alert = UIAlertController(title: "Done", message: "You beat the game", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
                    present(alert, animated: true)
                }
            }
        }
        else {
            //wrong
            let alert = UIAlertController(title: "Wrong", message: "You suck", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
            present(alert, animated: true)
        }

    }

    @IBAction func returnButtonTapped(_
        sender: UIButton) {
        dismiss(animated: true)
    }
}

struct Question {
    let text: String
    let answers: [Answer]

}

struct Answer {
    let text: String
    let correct: Bool //true / fasle
}

非常感谢

setupQuestions 函数的最后一行添加这一行:

gameModels.shuffled()

您可以通过将 gameModels.shuffle() 添加到 setupQuestions()

来随机排列问题