SWIFT - 致命错误:在展开可选值时意外发现 nil

Posted

技术标签:

【中文标题】SWIFT - 致命错误:在展开可选值时意外发现 nil【英文标题】:SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value 【发布时间】:2015-01-02 21:43:18 【问题描述】:

我正在制作一个简单的琐事应用程序来帮助学习该语言。目前我正在研究下一个问题 UIButton。我不知道我做错了什么。

func nextQuestion() 
    currentQuestion += 1

    var newQuestion: AnyObject = arrayOfQuestions[currentQuestion]
    questionCorrectAnswer = newQuestion[9].integerValue

    firstAnswer.hidden = false
    secondAnswer.hidden = false
    thirdAnswer.hidden = false
    fourthAnswer.hidden = false

    firstAnswer.setTitle("\(newQuestion[1])", forState: UIControlState.Normal)
    secondAnswer.setTitle("\(newQuestion[2])", forState: UIControlState.Normal)
    thirdAnswer.setTitle("\(newQuestion[3])", forState: UIControlState.Normal)
    fourthAnswer.setTitle("\(newQuestion[4])", forState: UIControlState.Normal)

    questionLabel.text = "\(newQuestion[0])"

    myNextQuestion.hidden = true


编辑:

var newQuestion : AnyObject = arrayOfQuestions[currentQuestion]

我的 newQuestion 变量设置为我的问题数组。

    let firstQuestion :AnyObject = Question(question: "What was the first planet to be discovered using a telescope, in 1781?", answerOne: "Mars", answerTwo: "Jupiter", answerThree: "Uranus", answerFour: "Mercury", correctAnswer: 3)
    let secondQuestion = Question(question: "Who averaged 1 patent for every three weeks of his life?", answerOne: "Ben Franklin", answerTwo: "Thomas Edison", answerThree: "Henry Ford", answerFour: "Ezra Gilliland", correctAnswer: 2)
    let thirdQuestion = Question(question: "Which island is the world's largest island?", answerOne: "Iceland", answerTwo: "Australia", answerThree: "Hawaii", answerFour: "Greenland", correctAnswer: 4)
    let fourthQuestion = Question(question: "What is the diameter of the Earth?", answerOne: "5,000 Miles", answerTwo: "6,000 Miles", answerThree: "8,000 Miles", answerFour: "10,000 Miles", correctAnswer: 3)
    let fifthQuestion = Question(question: "The US is the world's 5th largest producer of potatoes. What are the two top potato producing countries?", answerOne: "Canada/Italy", answerTwo: "China/Russa", answerThree: "China/Spain", answerFour: "Hawaii/Russia", correctAnswer: 2)
    let sixthQuestion = Question(question: "What is the symbol for iron on the periodic table?", answerOne: "Fe", answerTwo: "Ne", answerThree: "Se", answerFour: "Io", correctAnswer: 1)
    let seventhQuestion = Question(question: "The Hubble Telescope is named after which astronomer?", answerOne: "Frank Hubble", answerTwo: "Timothy Hubble", answerThree: "Edwin Hubble", answerFour: "Roger Hubble", correctAnswer: 3)
    let eighthQuestion = Question(question: "What year did the Apple's first iPhone become available?", answerOne: "2007", answerTwo: "2005", answerThree: "2005", answerFour: "2003", correctAnswer: 1)
    let ninethQuestion = Question(question: "OS computer abbreviation usually means what?", answerOne: "Optical Sensor", answerTwo: "Operating System", answerThree: "Open Software", answerFour: "Operating Sensor", correctAnswer: 2)
    let tenthQuestion = Question(question: "Where was the first mouse designed?", answerOne: "Apple", answerTwo: "Microsoft", answerThree: "Xerox", answerFour: "Hewlett-Packard", correctAnswer: 3)

    //Array of Questions Set

    arrayOfQuestions = [firstQuestion, secondQuestion, thirdQuestion, fourthQuestion, fifthQuestion, sixthQuestion, seventhQuestion, eighthQuestion, ninethQuestion, tenthQuestion]

当我在手机上运行应用程序时,我回答了问题并点击了下一个问题,应用程序崩溃了。我真的不知道错误告诉我什么。 请解释你的答案!

【问题讨论】:

您目前存在一些缺陷,但为了正确回答您的问题,我们需要查看一些变量的声明,以便查明错误实际发生的位置。 IE。 arrayOfQuestions 是如何声明的? ... questionCorrectAnswer 呢? newQuestion 不是 Question 的对象吗?不是这部分,questionCorrectAnswer = newQuestion[9].integerValue,应该是类似于 questionCorrectAnswer = newQuestion 的东西。正确答案?如果您向我们展示 Question 对象将会很有帮助。 【参考方案1】:

确保 newQuestion 数组不是 nil 并且它的索引 10 具有非零值。

如果我在这里提到的内容已经实现,请随时使用此数组的内容更新您的问题。

编辑

现在我可以看到更多你的代码,这很明显:

您说的是newQuestion[9].integerValue,但您应该使用newQuestion[5].integerValue。您要查找的整数值位于索引 5,而不是 9。

【讨论】:

newQuestion 数组中的所有值均未设置为 nil 显示newQuestion的内容。 newQuestion 被设置为我的 arrayOfQuestions 我到底为什么要这样做?当我这样做时,应用程序仍然崩溃【参考方案2】:

我可能是错的,但以下几行似乎是错误的:

var newQuestion: AnyObject = arrayOfQuestions[currentQuestion]
questionCorrectAnswer = newQuestion[9].integerValue

您声明newQuestion 是AnyObject 类型,它不是 数组类型,然后您在下一行取消引用它,就好像它是一个数组一样!这会触发致命错误!

我建议您将arrayOfQuestions 的声明更改为var arrayOfQuestions : Array<Question>() 之类的声明,并考虑将for 循环用于nextQuestion() 的其他一些功能。可能类似于以下内容:

// Build the array of Questions
var arrayOfQuestions = Array<Question>()

arrayOfQuestions.append(Question(question: "Why?", answerOne: "Because", answerTwo: "Whatever", correctAnswer: 2))
arrayOfQuestions.append(Question(question: "How?", answerOne: "Like this!", answerTwo: "Don't know", correctAnswer: 1))
// ... repeat until you have all questions you want


func nextQuestion() 
    currentQuestionNumber += 1
    let numberOfQuestions = countElements(arrayOfQuestions)

    // To look at the current questions specifics either do this ...
    let currQuestion: arrayOfQuestions[currentQuestionCounter]         
    let question = currQuestion.question
    let correctAnswer = currQuestion.correctAnswer

    // ... or something like
    let question = arrayOfQuestions[currenQuestionNumber].question
    let correctAnswer = arrayOfQuestions[currentQuestionNumber].correctAnswer

    for (var i = 0; i < countElements(arrayOfQuestions); i++) 
        if i <= currentQuestionNumber 
            answer[i].hidden = false
            answer[i].setTitle(arrayOfQuestions[i].question, forState: UIControlState.Normal
         else 
            answer[i].hidden = true;
           
     

 

【讨论】:

以上是关于SWIFT - 致命错误:在展开可选值时意外发现 nil的主要内容,如果未能解决你的问题,请参考以下文章

swift 2 致命错误:在展开可选值时意外发现 nil - 类别名称

Swift 和 UILabel - 致命错误:在展开可选值时意外发现 nil

(Swift) PrepareForSegue: 致命错误: 在展开可选值时意外发现 nil

Swift 错误 - 致命错误:在展开可选值时意外发现 nil [重复]

SWIFT - 致命错误:在展开可选值时意外发现 nil

swift coredata 致命错误:在展开可选值时意外发现 nil