Need help understanding the detailed benefits and patterns of using MVVM compared to just Views and ViewModels. My understanding of Models is that they are unnecessarily complicated but would like to know if I either misunderstand how to use them or if there are benefits I have not yet thought of.
Hello there,
To start off, I already understand the benefits of separating the logic of an application from the UI layer. I am currently working through the HackingWithSwift course and have a technical background as a product manager but am not a developer. I say that because I understand the HWS project I am working on does not require anything to adhere to the MVVM architecture pattern but I think I have a deeper technical understanding than most at this level and am choosing to so I can get the experience.
**Project Details**
I originally started with a ViewModel class and did not understand what the Model part needed to be. After coming across some articles, I decided to refactor my ViewModel class to put the logic inside of a Model struct. I instantiate this Model struct within my VM class but since my View layer should not know about the model, I created computed variables within my VM class.
Project branch w/ no Model file (original): [https://github.com/MichaelWDanko/100-Days-SwiftUI/tree/wordScramble-viewModel-no-model/WordScramble/WordScramble](https://github.com/MichaelWDanko/100-Days-SwiftUI/tree/wordScramble-viewModel-no-model/WordScramble/WordScramble)
Project branch w/ Model file refactor (new): [https://github.com/MichaelWDanko/100-Days-SwiftUI/tree/wordScramble-viewModel-refactor/WordScramble/WordScramble](https://github.com/MichaelWDanko/100-Days-SwiftUI/tree/wordScramble-viewModel-refactor/WordScramble/WordScramble)
This snippet below shows the entirety of the refactored ViewModel.
class WordScrambleViewModel: ObservableObject {
@Published var model = WordScrambleModel()
var rootWord: String { model.rootWord } var playerScore: Int { model.playerScore } var errorTitle: String { model.errorTitle } var errorMessage: String { model.errorMessage }
init() { }
func makeWordSubmission(_ word: String) -> Bool { let answer = SubmittedAnswer(word: word.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)) return model.makeWordSubmission(answer)
} // End of makeWordSubmission }
**Question**
If this is snippet and my other code on the refactored branch is acceptable, why would this be beneficial? It seems like the ViewModel layer is simply a proxy back to the Model and is unnecessarily more complicated?