|
I am learning SwiftUI and the basic MVVM pattern. For learning I came up with this code
I don't understand why my view is not updated. Should not eavery change of DataModel.data be published and the view redrawen. Please help me Thank you |
|
You have two
and in the
A little tip if you put the VM in a
and then you can do this
|
|
I think a more fundamental problem is that an @Published var must be a value type, not a reference type. SwiftUI updates the view when an @Published var changes. However, when the @Published var is a reference type object, i.e., an instance of a class such as |
|
Regarding MVVM, my impression is that there is no need to consider adhering to MVVM design to be virtuous goal. Neither Apple nor Paul Hudson ever refer to the concepts of MVVM or View Model. MVVM is a design approach that inspired SwiftUI, but SwiftUI employs its own, somewhat different approach. I think it's simpler to just think of ObservableObjects as what the name implies, objects whose values are observed (and modified) by views, without worrying about whether they adhere to some theoretical definition of a view model. Also, it seems to me that the structs that are typically considered "data models" in SwiftUI are not really data models because they don’t store data. They are data types used to organize the data that is stored in the ObservableObjects. |
|
I agree with
I tend start with M - Model and then V - View. When there are starting to be alot of logic in the Model file
View file
View Model
As I said I would not use the View Model for this but if I start adding more logic in the View then would look at this. Paul (I think) does not do this in videos and live steams as it would make them longer but when you listen to he does say that he would do thing slightly different. They are number of different architecture and you just use the one that you are conferable with. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.