超出范围索引崩溃 (Swift)

时间:2021-07-21 08:20:57

标签: swift xcode

在 questionBank 中的问题用完后,任何 True/False 输入都会导致应用程序崩溃 有没有我可以修改或添加的代码行来解决这个问题?

Swift Xcode

我已经把代码贴在下面了 对编码非常陌生,即使这可能很基本,但仍然无法在线找到解决方案

class Question{
    var stem:String
    var ans:Bool
    
    init(question:String, answer:Bool){
        self.stem = question
        self.ans = answer
    }
}



class Quiz{
    var questionBank:[Question] = []
    var score:Double = 0.0
    var fraction:Int = 0
    var index:Int = 0
    
    init(){
        questionBank.append(Question(question: "At 166 metres, Bukit Timah is Singapore's highest point.", answer: true))
        questionBank.append(Question(question: "Singapore's national anthem in microtext is on the back of the S$1,000 note.", answer: true))
        questionBank.append(Question(question: "Fort Canning Hill is Singapore's first botanic garden", answer: true))
        questionBank.append(Question(question: "Buildings in Singapore can be higher than 280m", answer: false))
        questionBank.append(Question(question: "Bukit Timah Nature Reserve is an ASEAN Heritage Park", answer: true))
        questionBank.append(Question(question: "The lowest temperature record in Singapore is 20.5°C", answer: false))
        questionBank.append(Question(question: "Singapore has more than 3000km of road", answer: true))
        questionBank.append(Question(question: "The Pan Island Expressway is 42.8km", answer: true))
        questionBank.append(Question(question: "Singapore has changed time zone 6 times since 1905", answer: true))
        questionBank.append(Question(question: "Singapore is made up of 63 islands", answer: true))
    }
    
    //Methods
    func showQuestion()->String{
        let question = questionBank[index].stem
        return question
    }
    
    func checkAnswer(input:Bool){
        if input == questionBank[index].ans{
            score += 10.0
            fraction += 1
        }
            index += 1
    }
    

}
var myQuiz = Quiz()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        resetQuiz()
    }

    @IBAction func trueBtn(_ sender: UIButton) {
        myQuiz.checkAnswer(input: true)
        goNext()
    }
    
    @IBAction func falseBtn(_ sender: UIButton) {
        myQuiz.checkAnswer(input: false)
        goNext()
    }
    
    func resetQuiz(){
        myQuiz.index = 0
        myQuiz.score = 0.0
        myQuiz.fraction = 0
            
        if myQuiz.questionBank.count > 0{
            displayLbl.text = myQuiz.showQuestion()
            
        }
        
        scoreLbl.text = "Score: 0.0"
        progressLbl.text = "1 of \(myQuiz.questionBank.count)"
    }
    
    func goNext(){
        scoreLbl.text = "Score \(myQuiz.score)%"
        progressLbl.text = "\(myQuiz.index) of \(myQuiz.questionBank.count)"
            
        if myQuiz.index < myQuiz.questionBank.count{
            displayLbl.text = myQuiz.showQuestion()
            
        }
        else{
            let alert = UIAlertController(title: "Quiz", message: "Score: \(myQuiz.score)% (\(myQuiz.fraction)/10)", preferredStyle: .alert)

            alert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
            alert.addAction(UIAlertAction(title: "Try again", style: .cancel, handler: {action in self.resetQuiz()}))

            self.present(alert, animated: true)
            }
    }
}

0 个答案:

没有答案