[ad_1]
Disclaimer:I’m an Android developer but had to delve into a bit of Swift for a KMM project.
I have a ViewModel set up like so:
class MyViewModel:ObservableObject {
...
var items Array = [Item]()
//the above array gets populated later on. Tried setting it as a state variable to be observed but for some reason the values never got updated
...
}
I have already been able to access some other variables of MyViewModel from my Content View (which is the file it is located in but outside the struct’s scope) like so:
struct ContentView: View {
@ObservedObject private (set) var viewmodel: MyViewModel
var body: some View {...}
...
}
However, when I try to access the MyViewModel variables from another struct with a similar setup, XCode shows the Cannot find type ‘MyViewModel’ in scope
Is there a way a ViewModel can be shared across multiple views on swift like it’s done in Android/Kotlin?
[ad_2]