简体   繁体   中英

SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value

I am making a simple trivia app to help learn the language. Currently I am working on the next question UIButton. I have no idea what I am doing wrong.

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

}

EDIT:

var newQuestion : AnyObject = arrayOfQuestions[currentQuestion]

my newQuestion variable is set to my array of questions.

    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]

When I run the app on my phone, I answer the question and I hit Next Question and the app crashes. I don't really know what the error is telling me. Please explain your answers!

Make sure that the newQuestion array is not nil and that it has a non-nil value for index 10.

Feel free to update your question with the contents of this array if what I mentioned here is already fulfilled.

Edit

Now that I can see more of your code, it's quite obvious:

You're saying newQuestion[9].integerValue but you should use newQuestion[5].integerValue . The integer value you're looking for is placed at index 5, not 9.

I might be wrong, but the following lines seems erroneous:

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

You declare that the newQuestion is of the type AnyObject, which is not an array type, and then you dereference it in the next line as if it was an array! This triggers a fatal error!

I would suggest that you change the declaration of arrayOfQuestions into something like var arrayOfQuestions : Array<Question>() and look into using for-loops for some of the other functionality of nextQuestion() . Maybe something similar to the following:

// 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;
        }   
     }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM