Learn Courses My Dashboard

Struggling with the programming 'theory'

Hi, I am loving the courses, however I started out feeling confident and quickly lost my way. I am really struggling to understand the basic structure and am confused with the different terminology.

I get what Views are used for, but I am struggling with the difference between ViewModels and Models?

@timmad

Hi Tim,

Welcome to the code crew community.

A ViewModel and a Model are Swift conventions that have been around for a while.

Consider a ViewModel as being a Manager of your Model and is the intermediary between your View and your Model. It has methods that manipulate the data and has Publishers that broadcast changes that have occurred so that any View that relies on that data will update to reflect that change.

Your Model defines what the data looks like.

I prefer the way Mark Moeykens describes a SwiftUI app. Rather than MVVM which is Model, View, ViewModel his approach is VOODO which is View, ObservableObject and Data Object. Makes way more sense to me.

Hi Chris, thanks so much for the explanation. So am I right in saying, the Model handles the data, the view is the UI, and the view model is an intermediary between the two?

Yes, that’s the “current” convention when creating a SwiftUI App.

Thanks Chris, that helps a lot, appreciate your time.

I would add it’s not a Swift convention, but a general programming design pattern.

Cause other programming languages use the pattern MVVM.

I would say the model represents your data, yes the view is the UI, and overall yes the view model is an intermediary between the two.

You can use it to get your model into some kind of format that you would want to display to your view. Because the data straight out of a database may not always be the most user friendly, looking. Your VM can format it to make it easier for the View (ie. for the user to understand)

1 Like