Norbert
Norbert

Reputation: 33

SwiftUI: I get a preview error in the Canvas when trying to load local JSON Data

I am currently trying to learn JSON starting with local Data in a .json file in my app. I following instructions online building a sample row which I then put into a List. The Data comes from a .json file that I created following the correct Format.

When I try to preview the work in the Canvas I get an error that says:

Cannot preview in this file - JSON Practice.app May have crashed.

I am new to SwiftUI and also new to dynamic Lists and JSON Data, so maybe someone out there in the community could show me where I am going wrong. I looked everywhere online but could not find an answer to my issue.

Searched the net for similar projects or tutorials, so far I can't see any difference in the code. I also checked out the SwiftUI Tutorials from Apple for Lists and Navigation which shows how to read data from a JSON file locally, but even though I am using the near enough same code I get this error.

My JSON File Looks like this:

[
    {
        "id": 0,
        "name": "Salzburg",
        "image": "salzburg",
        "detail": "A small but beautiful Town in upper Austria",
    },
{
        "id": 1,
        "name": "Vienna",
        "image": "Vienna",
        "detail": "The Capital of Austria",
    },
{
        "id": 2,
        "name": "Linz",
        "image": "Linz",
        "detail": "The third largest town in Austria",
    }
]

My Data file Looks like this:

import SwiftUI

let citiesData: [City] = load("cities.json")

func load<T: Decodable>(_ filename: String, as type: T.Type = T.self) -> T {
    let data: Data

    guard let file = Bundle.main.url(forResource: filename, withExtension: nil) else {
        fatalError("Couldn't find \(filename) in main bundle.")
    }

    do {
        data = try Data(contentsOf: file)
    } catch {
        fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
    }

    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
    }
}

My Decoder (not sure if that's what it's called) file Looks like this:

import SwiftUI

struct City: Hashable, Codable, Identifiable {
    var id: Int
    var name: String
    var image: String
    var detail: String
}

And my row file Looks like this:

import SwiftUI

struct CityRow: View {

    var city: City

    var body: some View {
        Text(city.name)
    }
}

struct CityRow_Previews: PreviewProvider {
    static var previews: some View {
        CityRow(city: citiesData[1])
    }
}

When previewing the Row file it should show the first city name, but instead it is giving me an error as mentioned above.

Upvotes: 2

Views: 1532

Answers (3)

MarcelWeidum
MarcelWeidum

Reputation: 145

For me, the json file wasn't copied to the bundle resources. To do that:

  • Click your project
  • Click your target
  • Select Build Phases
  • Expand Copy Bundle Resources
  • Click '+' and select your file.

Upvotes: 0

Norbert
Norbert

Reputation: 33

OK, These are the Moment where I guess I feel really stupid. Turns out the JSON file was badly Formate, After the Details Lines there shouldn't be a ,

Thanks for Reading and trying to help. I Hope the next Person can See this and sollte it quicker than me

Upvotes: 0

Sorin Lica
Sorin Lica

Reputation: 7636

There is only one value in the array from your json, try

struct CityRow_Previews: PreviewProvider {
static var previews: some View {
    CityRow(city: citiesData[0])
}
}

Upvotes: 2

Related Questions