owned this note
owned this note
Published
Linked with GitHub
# CS193P - Lecture 3: MVVM and the Swift type system - 95 mins
* Demo code at cs193p.stanford.edu
* Architecture
* MVVM - Desgin paradigm
* Swift Type System
#
## [00:50](https://youtu.be/--qKOhdgJAs&t=50s) MVVM
* SwiftUI pretty much does not work without MVVM
* It will be pretty bad to build an app using SwiftUI without MVVM
* Model denotes what your application is and does.
* The model should be the single source of truth.
* 
* All the truth of state thing is in the model
* View denotes how your application shows.
* The view would be stateless
* Reflects the model
* Declared: declarative coding is time insensitive, reliable, and provable, compared to imperative coding which need to track every source that changes the View as it builds view “steps by steps by steps”. Think var `SwiftUI.View.body: some View { get }``:
* Only 1 way to ask for as SwiftUI asks for rebuilding some View entirely
* No way to change as the view is a computed var, it’s built up at once
* Only reflects the corresponding model
* Locality of the code: the whole code to build up View is right in this body
* automatically observes publications.
* ViewModel is like a interpreter between Model and View, and it interprets the models for the view.
* The ViewModel also serves as a gatekeeper for the Model
* The ViewModel never sotres the data for the model inside of itself.
* The ViewModel must track all the chages in thet model.
* VM observes and publishes “something changed”. Why publish? VM has NO connection with Views that using it. Though it’s a model interpreter for Views, but it’s not View-specific.
* Most of the time, Model is pure data structure. For VMs that only facilitates the Model are called “Store”.
* some keywords related.
* 
* User may do some intent to change data in model, and the flow might be like the diagram.
* 
#
## [20:10](https://youtu.be/--qKOhdgJAs&t=20m10s) Swift Type system
* There are essentially 6 major different kinds of types in Swift. This class will only mention 4 of these types, e.g. struct, class, generics, and functions.
* 
#
## [21:30](https://youtu.be/--qKOhdgJAs&t=21m30s) struct and class
* 
* stored vars, stored in memory
* computed vars, like body var
* constant lets, whose value never change.
* functions.
* Free init initialize **ALL** vars in struct, however class initialize **NO** vars in which is totaly different.
* We always declare a ViewModel with class becuase we want to **share** the ViewModel between multiple views.
* **Intent functions** for people to mutate ViewModel to make the ViewModel more controlled
#
## [35:40](https://youtu.be/--qKOhdgJAs&t=35m40s) Generics
* "don't care" type (we call this feature "genrics", official name is "type parameter")
* Ausome example of generics: **Array**
* It's perfectly legal to have multiple "don't care" types in the generic **<>**.
* Swift combines this with protocols to take it all to the next level. We will talk about that next week.
#
## [42:10](https://youtu.be/--qKOhdgJAs&t=42m10s) Functions
* 
* Function could be a type of parameter.
* Passing functions to other functions, that is the heart of functional programming.
* 
* You will call the function with parameter with no label("**operand**").
#
## [48:20](https://youtu.be/--qKOhdgJAs&t=48m20s) Apply all those things above to the Memorize Game
* 
* Apply MVVM thing to our app.
* ViewModel make the model to be private to protect the model against any View reaching in and trying to change this.
* What a property initializer is? When we initialize a property with a "=" after the property declaration, which means we utilize the property initializer.
* initializer timing
* static let, type property or type variable.
* static fun, type function.
* generics