'Is there a way to check if @Environmentobject exists swiftui
I have a view hierarchy where sometimes there is a chance that an @EnvironmentObject is not resolved. Is there a way to check if such @EnvironmentObject exists before accessing its value?
Something like below:
struct SampleView: View {
@EnvironmentObject var someStateObj: SomeStateObjectType
var body: some View {
Button("Test") {
// Line below will not work...
if someStateObject != nil { /* Do Something */ }
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|