Landmarks Tutorial. Preview doesn't work

Hi everyone,

I'm trying to follow the Landmarks project tutorial and got stuck in the second part, in the second section (Create the Row View), step 6 where it says "Modify the text view to use the landmark property’s name." This must be done in the LandmarkRow.swift file.

https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation

The tutorial shows that after doing this, the preview will display the name of the landmark (Turtle Rock) instead of the usual "Hello world" greeting. But my replacement is not happening. Moreover, from this point on, the preview stops working. There are no error messages in the code, except for the message that the preview could not be performed.

I checked and rewrote the code several times, replaced the data source files, but nothing helped.

At the same time, the preview works well in other view files.

I can't figure out what's wrong with my code? Any ideas as to what the reason will be is appreciated.

Below is the code of two files, the LandmarkRow.swift, where view does not work, the second is ModelData.swift and it is related to the previous one.

LandmarkRow.swift


Code Block
import SwiftUI
struct LandmarkRow: View {
    var landmark: Landmark
    var body: some View {
        HStack {
            landmark.image
                .resizable()
                .frame(width: 50, height: 50)
            Text(landmark.name)
            Spacer()
        }
    }
}
struct LandmarkRow_Previews: PreviewProvider {
    static var previews: some View {
        LandmarkRow(landmark: landmarks[0])
    }
}

ModelData.swift
Code Block
import Foundation
var landmarks: [Landmark] = load("landmarkData.json")
func load<T: Decodable>(_ filename: String) -> 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)")
    }
}


Replies

As far as I tried your code, the preview of LandmarkRow is shown as expected.

So, if there is something wrong in your project, it may exist in other parts of your project.
Have you checked all other files or project settings?
Thank you for your help.

Yes, I've checked all the files, but it looks like I'm missing something. I will try to redo the project again.

Yes, I've checked all the files, but it looks like I'm missing something. 

Thanks for your reply.

Unfortunately, Xcode sometimes shows weird behaviors.
You may know and already have tried, but restarting Xcode or your Mac, and/or Clean and Re-Build (Produce > Clean Build Folder, Cmd-B) would solve some sorts of issues.

If you can Run your code, do not use preview would be a temporary workaround.

Anyway, many experienced developers are still suffering from Xcode's unkind or weird behavior.
Hope you can solve your issue soon and create great apps. Good luck.