Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Assigning a struct instance to UILabel

Hello!

I keep getting errors with my code when trying to chance an UILabel with a text string

First I created a struct:

        struct Question {
             var  question : String?
             var  answer : String?
         }

Then made a instance of the struct:

         var questionsArray = [Question]()

Then made a few test-purpose-only questions inside viewDidLoad:

        questionsArray = [Question(question: "2 + 2", answer: "4"),

        Question(question: "2 + 3", answer: "5"),

        Question(question: "2 + 4", answer: "6"),

        Question(question: "2 + 5", answer: "7"),

        Question(question: "2 + 6", answer: "8"),

        Question(question: "2 + 7", answer: "9"),

        Question(question: "2 + 8", answer: "10")]

Then I made a func that should change the UILabel.text, as the level int increases each time the user types in the right answer:

    func displayQuestion() {

    if questionLabel.text == questionsArray[level].question {
        //Do Nothing
    } else {
        //Display or update the question
        questionLabel.text = questionsArray[level].question
    }

And ofcoure, I added an

   displayQuestion()

inside the viewDidLoad after the questions to start the function

No errors, and I feel like the code should be safe and actually work, but when I try to build the app in the simulator I keep getting "fatal error: unexpectedly found nil while unwrapping an Optional value" error.

Can you see any mistakes or some code that causes the app to crash?

Thanks!

1 Answer

Adrian, you define question as a String optional

var question: String?

but then in this statement:

questionLabel.text = questionsArray[level].question

you return a question (an optional) without unwrapping it.

Here's your code modified to run in a playground:

        struct Question {
            var  question : String?
            var  answer : String?
        }

        var questionsArray = [Question]()

        questionsArray = [Question(question: "2 + 2", answer: "4"),

            Question(question: "2 + 3", answer: "5"),

            Question(question: "2 + 4", answer: "6"),

            Question(question: "2 + 5", answer: "7"),

            Question(question: "2 + 6", answer: "8"),

            Question(question: "2 + 7", answer: "9"),

            Question(question: "2 + 8", answer: "10")]

        var text = ""

        func displayQuestion() {

            if text == questionsArray[0].question! {
                //Do Nothing
            } else {
                //Display or update the question
                text = questionsArray[1].question!
            }
        }

        displayQuestion()

Here forcibly unwrapping works (and I used it just to make the point) because your data is complete, but in general it's a bad idea and one should use if let or guard to be safe!!