Repeated Crash Reports when I add code to a project

I am doing the Swift Landmarks app tutorial on Xcode 13.2.1 on my 2019 Macbook Pro. I am up to the Working with UI Controls section. Every time I add the following lines of code to ProfileSummary.swift:

@EnvironmentObject var modelData: ModelData

.
.
.

Divider()
         
        VStack(alignment: .leading)
        {
                  Text("Recent Hikes")
                    .font(.headline)

                  HikeView(hike: modelData.hikes[0])
        }

and I then try to view the preview I repeatedly get a crash report and everything stops working. When I remove those lines of code everything goes back to working fine.

The crash report is in this attachment:

^ That is the translated report. If you guys would like the full report I will post it. I am stuck and don't know what to do any help will be appreciated!

Answered by Claude31 in 705006022

Could you show the complete code, problem may come from elsewhere. Notably the Preview file. And tell where you get the crash.

Preview struct should be like this:

struct LandmarkDetail_Previews: PreviewProvider {
    
    static let modelData = ModelData()
    
    static var previews: some View {
        
        LandmarkDetail(landmark: modelData.landmarks[0])
            .environmentObject(modelData)
    }
}

It could be you forgot the .environmentObject(modelData)

Note: next time, choose more appropriate tags. That's not an iOS issue, nor Swift, nor Xcode Server but definitely a SwiftUI. That will help you get more and better answers.

Accepted Answer

Could you show the complete code, problem may come from elsewhere. Notably the Preview file. And tell where you get the crash.

Preview struct should be like this:

struct LandmarkDetail_Previews: PreviewProvider {
    
    static let modelData = ModelData()
    
    static var previews: some View {
        
        LandmarkDetail(landmark: modelData.landmarks[0])
            .environmentObject(modelData)
    }
}

It could be you forgot the .environmentObject(modelData)

Note: next time, choose more appropriate tags. That's not an iOS issue, nor Swift, nor Xcode Server but definitely a SwiftUI. That will help you get more and better answers.

Repeated Crash Reports when I add code to a project
 
 
Q