App Crashing every time I try to Preview it on my Canvas!

Hey Fellow Community members. I have a very repetitive bug that annoys me every time I try to create a new app. I am currently on module 4 of Chris’ iOS foundations course, and I have paused my learning in lesson 10. However, this error has got nothing to do with the course, its something I encountered while trying to create a basic app that displays the famous food dishes around the world in a list, and when clicked on the list, it takes the user to a detailed view displaying the dish origin, cuisine, and a brief description of the dish. But whenever I am adding something either to my View-model or my view, and trying to preview how the data looks in the canvas, the app is crashing…and I am getting the error, as “could not parse JSON data in scope”. Will provide the screenshot here:-

The Model of my JSON data:

The Extension Bundle for Parsing the JSON data:

The ViewModel of JSON data, which when I am trying to preview on my canvas, app is crashing:

The View (ContentView) of my app (keeps crashing whenever I hit preview):

The Crash Log:

These are the Bugs! I humbly request anyone viewing this to help me out, since I am not able to proceed forward with the rest of the course until this problem is sorted out. I asked the same question before regarding this app crash on this same “App Development”, under “Module 4, Lesson 5”, but I did not receive an answer…therefore hoping for some help this time around. Thank you!

You have an error in your JSON parsing. But you can’t see what that error is because your Decode function masks any error by using try? to convert errors to optionals.

You should do something like this instead:

func decode(_ file: String) -> [Food] {
    ... //keep what you have to get url and data

    //and then replace the actual decoding with:
    do {
        let loadedData = try JSONDecoder.decode([Food].self, from: data)
        return loadedData
    } catch {
        print(error)
        return []
    }
}

This will catch and print out what the error is so you can fix it.

And for future reference:

When posting code to these forums, please post the code as text rather than an image. This makes it far easier to read (some of us have aging eyes!) and also makes it possible for other posters to copy/paste the code in order to test solutions and such without having to retype all your code from scratch.

To post your code as text, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

Alright let me see it thank you

Sure I will keep that in mind, thanks for replying appreciate it!