如何将用户输入与Array的随机对象进行比较

时间:2019-05-12 18:55:33

标签: swift

我有一堆与我的自定义课程“问题”相关的问题

class QuestionBank {
    var list = [Question]()


    init() {
        // Creating a quiz item and appending it to the list
        let item = Question(text: "2² = ", correctAnswer: 4)

        list.append(item)

        list.append(Question(text: "3² = ", correctAnswer: 9))
        list.append(Question(text: "4² = ", correctAnswer: 16))
        list.append(Question(text: "5² = ", correctAnswer: 25))

        // and so on...
    }
}

这是自定义类  问题

let answer : Int
let questionText : String

init(text : String, correctAnswer : Int) {
    questionText = text
    answer = correctAnswer
}

我有一个按钮,当您按一下它时,所有问题(等于QuestionBank)都被打乱了。接下来,用户输入答案

@IBAction func truePressed(_ sender: Any) {
    allQuestions.list.shuffle()
    questionLabel.text = allQuestions.list[questionNumber].questionText
    if Int(textField.text!) == allQuestions.list[questionNumber].answer {
        print("yes")
    }
    textField.text = ""
}

问题是控制台不输出任何内容。我认为这是因为shuffle()发生时,“ questionText”和“ answer”都在改组,还是我错了?如果我是对的,我该如何更改'allQuestions'改组,以便当您按下按钮时,textField读取用户输入,然后将input(textField.text)与'allQuestions.list [questionNumber] .answer'比较?

0 个答案:

没有答案