The MVVM Pattern
Separate view, view model, and model concerns.
What Is MVVM?
MVVM stands for Model-View-ViewModel. It is an architectural pattern that separates your app into three layers, each with a single clear responsibility.
- Model — data and business rules
- View — what the user sees
- ViewModel — the bridge that prepares Model data for the View
The goal is separation of concerns: each layer changes for its own reason, independently of the others.
The Model Layer
The Model holds your raw data and the rules that govern it. It knows nothing about the screen or how things are displayed.
Models are usually plain structs or domain objects. They are easy to test because they have no UI dependencies.
struct User {
let id: UUID
let firstName: String
let lastName: String
let isPremium: Bool
}
struct Order {
let items: [String]
let total: Decimal
}