Learn Courses My Dashboard

Mod 4 Lesson 8 Fatal error: No ObservableObject of type Model found

I don’t know what I am missing. When I press ⌘b the build is successful.
When I run the program in the simulator I get the following error:

“Thread 1: Fatal error: No ObservableObject of type Model found. A View.environmentObject(_:slight_smile: for Model may be missing as an ancestor of this view.”

Thanks for looking!
Monty

Here is the code in ContentView.swift

struct ContentView: View {
    
    @EnvironmentObject var model: Model
    
    var body: some View {
        
        TabView{
            VStack{
                Text("People")
                List(model.people) {person in
                    Text(person.company)
                }
            }.tabItem{Image(systemName: "pencil")
            }
        }
    }
}

Here is the code in ContentView.swift


import Foundation

class Model : ObservableObject {
    
    @Published var showName = true
    @Published var showAddress = true
    @Published var showCompany = true
    @Published var showYears = true
    
    var people = [
        Person(
            name: "Inigo Montoya",
            address: "555 Youkilledmyfather Street",
            company: "CodeWithChris",
            yearsOfExperience: 35
        ),
        Person(
            name: "Edna Mode",
            address: "123 Nocape Lane",
            company: "CodeWithChris",
            yearsOfExperience: 177
        ),
        Person(
            name: "Travis Bickle",
            address: "99 Youtalkingtome Road",
            company: "CodeWithChris",
            yearsOfExperience: 99
        ),
        Person(
            name: "Walter Sobchak",
            address: "8 Dude Place",
            company: "CodeWithChris",
            yearsOfExperience: 23
        ),
        Person(
            name: "Julius Winnfield",
            address: "25 Ezekiel Ave",
            company: "CodeWithChris",
            yearsOfExperience: 17
        )
    ]
    
}

struct Person : Identifiable {
    var id = UUID()
    var name: String
    var address: String
    var company: String
    var yearsOfExperience: Int
}

Here is the error msg

Have you added a Model object into the environment before trying to get one out?

Assuming that ContentView is the root View of your application, you would need something like this in your App struct:

ContentView()
    .environment(Model())

That fixed it.
(so confusing)
Thanks so much!!
Monty

@main
struct __6_try_twoApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(Model())
        }
    }
}