I have an array of MyItem.
var array:[MyItem]
I was expecting that to fail, but I have tried to pass array using
.environmentObject(array)
Obviously it failed.
Instance method ‘environmentObject’ requires that ‘[MyItems]’ conform to ‘ObservableObject’
Is there a way to do that?
>Solution :
Wrap the array inside a class conform ObservableObject, and then you can pass it normally
final class ModelData: ObservableObject {
....
//Add @Published to enable binding to any views using this property
@Published var array: [MyItem] = []
}
@StateObject private var modelData = ModelData()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(modelData)
}
}