|
Hi everyone, I'm currently on last day of project 14: https://www.hackingwithswift.com/books/ios-swiftui/bucket-list-wrap-up With the latest challenge (for 2021-2022) being as follow:
Here is how I've modified my EditView:
And my added EditView-ViewModel:
It works but... I'm not sure it's the best practice? Anyone has an advice on this? Particularly about those lines (I'm not sure if it's supposed to be done like that):
Thanks in advance for any feedback! |
|
|
|
I did not look at this too closely. But here's a very high level comment. I remember from view and viewmodel lessons that Views should be very streamlined and simple. They should not have to do much thinking. Views should not know about the internals of your business. Here's an example from your code:
Consider training yourself to think, I am a Save button. What are my intentions? Answer: I intend to save a new location. I am a View, and this is not my job. I need to send the form data to the view's model. The ViewModel is the best place to validate a location, and save it to CoreData. I think it's Paul Hegerty (CS193p) who says ViewModels should contain Intents. (minute 19:18) See: CS 193p Lesson 4 What do you intend for your model to do? (19:18) Prof Hegerty is a fan of having models be completely private. He only exposes model data via the viewmodel's vars and funcs. (7:41) These words stuck with me: "This is what the view code should look like. Ask the ViewModel to express the user's intent." You intend for the view model to save this new location. You are changing your model. Consider only allowing the viewmodel to do this via a function. Consider:
Not sure if this is helpful observation? Interesting adding the ViewModel as an extension. So you've made great progress. But you asked for any feedback. So, voila! |
|
This initializer:
can be written as this:
That's how Paul does it in his solution. |
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.