I have a class that needs to update a global environment object. I can pass that environment object between my structs all day, but how do I allow a class object to access the same variable?
import SwiftUI
class Global: ObservableObject
{
@Published var num = 10
}
class MyClass:ObservableObject
{
@Published var mode = 1
@EnvironmentObject var global: Global
func updateMode()
{
self.mode += 1
global.num += 1
}
}
@main
struct MyApp: App
{
let settings = Global()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(settings)
}
}
}
struct ContentView: View
{
@EnvironmentObject var global: Global
@ObservedObject var myClass = MyClass()
var body: some View
{
VStack
{
Text("Setting \(global.num)")
Text("Mode \(myClass.mode)")
Button("Click Me", action: {myClass.updateMode()})
}
.padding()
}
}
The following code gives an error:
Fatal Error: No ObservableObject of type Global found. A View.environmentObject(_:) for Global maybe missing an ancestor of this view.
I could pass the global object into myClass.updateMode, but then it doesn't seem very global at that point? I would have thought there must be a better way.
A possible approach is to make it shared (and don't use @EnvironmentObject
anywhere outside SwiftUI view - it is not designed for that):
class Global: ObservableObject
{
static let shared = Global()
@Published var num = 10
}
class MyClass:ObservableObject
{
@Published var mode = 1
let global = Global.shared // << here !!
// ...
}
@main
struct MyApp: App
{
@StateObject var settings = Global.shared // << same !!
// ...
}