Xcode 7.2 fatal error: unexpectedly found nil while unwrapping an Optional value

Xcode 7.2 fatal error: unexpectedly found nil while unwrapping an Optional value

我正在编写一个 QuizApp。我不断收到错误:"fatal error: unexpectedly found nil while unwrapping an Optional value" 在行中:“QLabel.text = Levels[LevelNumber].Questions[QNumber].Question”。我不明白这一点。我还多次添加和删除了 Outlet。

    import UIKit
    import SpriteKit

    struct Question {
        var Question: String
        var Answers: [String]
        var Answer: Int
    }

    struct Level {
        var Questions = [Question]()
    }

    class ViewController: UIViewController {
        @IBOutlet var QLabel: UILabel!
        @IBOutlet var Buttons: [UIButton]!

        var QNumber = Int()
        var AnswerNumber = Int()
        var Levels = [Level]()
        var LevelNumber = Int()

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

        func startGame() {
            //Level1
            // Question1
            let Qquestion1 = "What's the name of America's president?"
            let Answers1 = ["Barack Obama", "Donald Duck", "Micky Maus", "Peter Pan"]
            let Question1 = Question(Question: Qquestion1, Answers: Answers1, Answer: 0)

            // Question2
            let Qquestion2 = "How old am I?"
            let Answers2 = ["10 years old.", "20 years old", "5 years old.", "1 year old."]
            let Question2 = Question(Question: Qquestion2, Answers: Answers2, Answer: 2)

            // Question3
            let Qquestion3 = "Who won 2014 the 'Soccer Worlcup'?"
            let Answers3 = ["Austria", "USA", "Mongolia", "Germany"]
            let Question3 = Question(Question: Qquestion3, Answers: Answers3, Answer: 3)

            // Question
            let Qquestion4 = ""
            let Answers4 = ["", "", "", ""]
            let Question4 = Question(Question: Qquestion4, Answers: Answers4, Answer: 1)

            let LevelExample = Level(Questions: [Question1, Question2, Question3, Question4])

            // LevelScience
            // Question5
            let Qquestion5 = ""
            let Answers5 = ["", "", "", ""]
            let Question5 = Question(Question: Qquestion5, Answers: Answers5, Answer: 2)

            // Question6
            let Qquestion6 = ""
            let Answers6 = ["", "", "", ""]
            let Question6 = Question(Question: Qquestion6, Answers: Answers6, Answer: 0)

            // Question7
            let Qquestion7 = ""
            let Answers7 = ["", "", "", ""]
            let Question7 = Question(Question: Qquestion7, Answers: Answers7, Answer: 1)

            // Question8
            let Qquestion8 = ""
            let Answers8 = ["", "", "", ""]
            let Question8 = Question(Question: Qquestion8, Answers: Answers8, Answer: 3)

            let LevelMoviesLiterature = Level(Questions: [Question5, Question6, Question7, Question8])

            Levels = [LevelExample, LevelMoviesLiterature]
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        @IBAction func ExampleLevelButton(sender: AnyObject) {
            exampleCategory()
        }

        func exampleCategory() {
            LevelNumber = 0
            startGame()
            PickQuestion()
        }

        func PickQuestion() {
            if Levels[LevelNumber].Questions.count > 0 {
                QNumber = random() % Levels[LevelNumber].Questions.count
                QLabel.text = Levels[LevelNumber].Questions[QNumber].Question

                AnswerNumber = Levels[LevelNumber].Questions[QNumber].Answer

                for i in 0..<Buttons.count {
Buttons[i].setTitle(Levels[LevelNumber].Questions[QNumber].Answers[i], forState: UIControlState.Normal)
                }

                Levels[LevelNumber].Questions.removeAtIndex(QNumber)
            } else {
                NSLog("Done!")
            }
        }

希望得到帮助! 谢谢N.F.M.

您调用 startGame,它会在您调用 PickQuestion 之后 设置您的 Levels 数组 。因此,当数组仍然为空时,您访问索引 0 处的元素。

你必须先初始化:

    LevelNumber = 0
    PickQuestion() // call here -> error
    startGame() // init here

更改为:

LevelNumber = 0
startGame() // init here
PickQuestion() // call here