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)")
    }
}


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.

I have face the similar issue and for me the issue is I have typo issue in Landmark.swift and because of that the parsing is not happening.

I also could not get Preview to work at this point, and wasted days investigating. Now (July 2023) there have been 1.6k views of this so it must be a common problem.

I have managed to resolve it, by loading the finished sample code and going through in detail. I found that landmarkData.json was missing from the bundle, causing the load error.

I went back to the root of the code (the top item in the Navigation pane), which opens the .xcodeproject file, and chose Landmarks in the TARGETS section on the left, then across the top chose the Build Phases.

Scroll to the very bottom to view the Copy Bundle Resources.

landmarkData.json was missing from that list, so it wasn't being copied into the Bundle. Click the +, and select landmarkData.json in the Navigation tree, then click Add and it should now be in the list.

That immediately resolved my problem - Preview worked everywhere its shown.

My answer is below...

The issue is in the landmarkData.json file, the problem comes from the tutorial page.

If you see, landmarks[0] gets you imageName = turtlerock

Now, if you go to Assets and see the name of the file it's named turtleRock, with capital R.

If you go to the JSON file and change the naming to turtleRock your preview will work.

For every other object on that JSON file, it works correctly

Had the same issue. I think the problem was, that i moved the landmarkData.json file around during the tutorial..

I deleted the landmarkData.json file and added it back. Then it worked.

Landmarks Tutorial. Preview doesn't work
 
 
Q