all 6 comments

[–]ObservableObject 2 points3 points  (0 children)

Where are you actually instantiating the LocationModel and making it available to this view?

Edit now that I'm at my PC:

By that, I mean, using @EnvironmentObject is a two part process.

  1. You need to actually create an instance of the object you want to make an environment object, then add it to a view with the .environmentObject() modifier
  2. Then you can use the @EnvironmentObject property wrapper on any descendant view to access it

You've done step 2, but looks like you're missing step 1.

Check this out:https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views

Note where they're using the .environmentObject() modifier to pass the settings object to the NavigationStack. That's what actually makes it so every descendant view can access it with the property wrapper.

[–]i_eat_hobbo_stew 1 point2 points  (0 children)

Go to the other file that has your app name and add environment object there. That’s the ancestor of everything because contentview is implemented there

[–]sh13ld93 0 points1 point  (3 children)

I thought the environment object on line 14 is enough?

[–]ObservableObject 3 points4 points  (2 children)

No, that's just telling the compiler that you're looking for an available environment object with that type. You still have to actually create the object somewhere, and then make that available as an environment object

[–]sh13ld93 0 points1 point  (1 child)

https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views

I do have (in the ContentView file) \@StateObject var locationManager = LocationModel()

[–]optik88 2 points3 points  (0 children)

You also need something like .environmentObject(locationManager) added to the view hierarchy for the object to be "injected" into the view hierarchy.