I simply want to pass two EnvironmentObjects to a view which are different instances of the same class. I feel like I have overlooked something as this seems quite simple.
The most obvious way would be to do it the usual way (which works if they are instances of a different class) like so:
Top-View:
exampleView()
.environmentObject(vm1)
.environmentObject(vm2)
Sub-View:
struct exampleView: View {
@EnvironmentObject var vm1: ViewModel
@EnvironmentObject var vm2: ViewModel
var body: some View {
...
}
}
But this doesn't work as the view will set both vm1
and vm2
to the original vm1
.
I have searched everywhere but could not find any solution except to use a single class and change the properties of the class for example from "property" to "property1" and "property2". This is not only quite ugly code but also not possible in my case.
Why? The project I am working on is quite big and I do not have access to some of the code. The class which is initialised twice owns an array which is filled differently, depending on other factors. In the view, some parts are being reused because the array has the same kind of items, but filled with different content.
If this seems really wrong for Swift UI please tell me. I would have to remake a lot, but on the surface this seemed to be quite a simple problem, so I would appreciate answers where I can keep the structure of my class.