Hi,
I think you did fine work on tutorial. My concern is that MVVM doesnât work well with SwiftUI, especially for beginners.
Iâll clarify some of the points you mentioned.
First, sorry for confusion. I meant to say class WeeklyWeatherViewModel: ObservableObject
is a reference type view model, whereas struct WeeklyWeatherView: View
is value type.
The fundamental difference of opinion is that is struct WeeklyWeatherView: View
a view or a model?
You think itâs a view because it conforms to View. I think itâs a model that can be used to construct a view.
Iâd argue that a view like Button needs to be reference type because it has mutable properties like title, color, âŠetc. So the fact that WeeklyWeatherView
is a value type makes it a model. A naming of struct WeeklyWeather: View
would make this model-view map relation more clear.
For nesting views, I think thereâs a difference between nesting views
and nesting view models
. Nesting views are common of course. But your view models are not views. They donât even conform to View. And the worst of all, they may contain side effects like networking. In old days, UIViewController clearly cannot be used as view model so thereâs no risk of nesting view model (at least not immediately).
But SDK changes. From what I can tell, SwiftUI has some of the MVVM features built-in. For argumentâs sake letâs assume struct WeeklyWeather: View
is indeed a view model. Now do you create a new view model or just straight up use it?
As for nested view model being âacceptedâ. Iâd say from my experience it often makes the code more complicated, so not accepted by me personally. Iâm sure it has some merits, and the point Iâm trying to make here it to provide an alternative. Developers should make their own calls.
Now for class WeeklyWeatherViewModel: ObservableObject
being a reference type. Yes you can use access control or other means for better safety. But the point Iâm making is that itâs dangerous for beginners because they donât know they need to. Even experienced developers would often abuse reference type.
I think SwiftUI doesnât care if you have âgood intentionsâ. Immutable type and compiler check are the safest bets, which is why I say view model should be value type and that type is required by SwiftUI by default. These are design patterns accepted by SwiftUI itself.
Lastly, as you said, the tutorial may have to change name if you remove all view models.
Iâm not asking you to. A comparison is a good thing. But I do think MVVM may not be as âgoodâ as before SwiftUI, and this needs to be pointed out.
Well, itâs not like a lot of people would read comments anyway
So I think Iâll leave the discussion here. People who read this can make up their own minds.
Thanks