Updated for Xcode 14.2
When you’re using environment objects, SwiftUI automatically connects your properties by scanning the environment for matching types. So, if you have an environment property of type User
, SwiftUI expects to find an instance of that class in the environment before it is used.
This error occurs because no matching object could be found, which is usually a result of you forgetting to put it into the environment, or putting it into the wrong environment. Remember, you can have multiple environments active in your SwiftUI app: you might have individual environments for each of the tabs in a TabView
for example.
To fix the problem, make sure the view that causes the crash has the correct environment set, like this:
YourView().environmentObject(yourEnvironmentObject)
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.