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)
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.