|
When using @EnvironmentObject, can you have an @EnvironmentObject inside of another class? I have a setup (MeetingStats) class and then a processing class(Meeting). The processing class and the setup classes are both displayed via different tabs in my app. So if you can change the settings in setup, you should see in real time the changes to the processing class (that is mostly timer driven). I have tried to switch from using the AppDelegate to passing things between views, to using @EnvironmentObject. I have changed my SceneDelegate to create the initial objects, and am passing them .envrionmentObjects to the ContentView. However, when I run the app, the first time I try to access the setup class in the processing class, I crash with the message No ObservableObject of type MeetingStats found. A View.environmentObject(_:) for MeetingStats may be missing as an ancestor of this view. I am not sure if I have the right understanding of how to use this. SceneDelegate
ContentView
Meeting Class (this is the crash)
Is the issue I am not explicitly passing the values to the tabs from the contentView?
Explicitly passing the environment objects but that doesn't seem to make a difference, and I didn't think that was required as Swift was to look in the object hierarchy to find the object. |
|
This is the one thing that really bugs me about SwiftUI, alright it is one of the many many things. I was hoping this would be "fixed" in SwiftUI 2.0. You cannot use EnvironmentObjects in ObservedObjects, however as they are just other ObservedObjects themselves, you can pass them in at creation in your top level file. Obviously you will need to create an initialiser for Meeting that takes a MeetingStat.
They can both still be passed as EnvironmentObjects for use in Views. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.