I have viewmodel and view i have return some API logic in viewmodel and getting one dictionary after some logic..i want to access that dictionary value from view for ex.
viewmodel.someDic
but for now every-time i am getting empty dic.
class Viewmodel: ObservableObject {
@Published private var poductDetails:[String:ProductDetail] = [:]
func createItems(data: ProductRootClass) {
var productDetils = [String: SelectedProductDetail](){
//some logic
productDetils // with some object
self.poductDetails = productDetils
}
}
}
struct View: View {
@StateObject var viewModel: ViewModel
var body: some View {
VStack(alignment: .leading) {
Text("(viewModel.poductDetails)")
}
.onAppear(perform: {
print("(viewModel.poductDetails)")
})
}
}
I want to access this dictionary from view.
I tried accessing by returning productDetils from any function but get empty everytime.
may i know the way to access property from viewmodel to view?
3
Answers
You need a class conforming to
ObservableObject
and a property marked as@Published
Whenever the property is modified the view will be updated.
In the view create an instance and declare it as
@StateObject
I would prefer an array as model, it’s easier to access
You can make your viewModel as a Singleton.
Access this viewModel Singleton in the view
You need to get rid of the view model and make a proper model.
Now you can do
ForEach(model.productDetails)