|
I'm using the code from Paul's article "How to use @EnvironmentObject to share data between views". I just updated to xcode 11.4. This example crashes every time if you click "Increase Score" first followed by "Show Detail View". If I click "Show Detail View" first, it navigates to the view and I can navigate back. If I do that sequence 3, 4, or 5 times it crashes. The error is "Fatal error: No ObservableObject of type UserSettings found. A View.environmentObject(_:) for UserSettings may be missing as an ancestor of this view." This example works as expected, however, in xcode 11.3. Anyone else seeing this issue? I noticed it first in my own project that stopped working and found this simple example from Paul as a testbed. // SceneDelegate class UserSettings: ObservableObject { @Published var score = 0 } class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// ContentView struct ContentView: View { @EnvironmentObject var settings: UserSettings
} struct DetailView: View { @EnvironmentObject var settings: UserSettings
} |
|
|
|
Hi again, After I found this Paul's tweet (https://twitter.com/twostraws/status/1146315336578469888?s=20) I tried to send environment objects to NavigationLink as well. Now it works. It may help you too. It seems it's a bug and I'm really tired on struggling SwiftUI bugs. :( |
|
This has been bugging me as well. Everything runs fine in my simulator, but the SwiftUI preview does not work and it's frustrating as that is supposed to be one of the perks of SwiftUI. I get this:
Even though I've passing everything appropriately in the SceneDelegate and in each View Preview |
|
In the
|
|
I have a
This was breaking a number of my View previews and I managed to fix it by instatiating the 'ScreenCoordinator' class in the preview function as follows:
|
|
Did you try to use @StateObject?
Then pass it as an environmentObject:
Note: @StateObject is only available in iOS 14 (SwiftUI 2.0). You can find more info here. If that didn't work, just try to add self when you pass it:
|
|
We are using SwiftUI "2.0". Firebase reported a crash with our app: Fatal error: No ObservableObject of type Observer found. A View.environmentObject(_:) for Observer may be missing as an ancestor of this view.: file SwiftUI, line 0. The user was on iOS 14.01, but I have not been able to reproduce the issue on iOS 14.1 or 14.2. I have not tried 14.01 yet. However as I have seen this error reported in other forums and it seems to be a bug with SwiftUI when environmentObject is not used with a view created by NavigationLink. The recommend solution was just to use environmentObject, but that seems to defeat the purpose of using the environment to store objects in the first place. Does anyone know of any offical Apple bug fixes where this is addressed? If it's been fixed, then I won't worry about it in older iOS releases. Thanks. |
SPONSORED From January 26th to 31st you can join a FREE crash course for iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.