Designing a ViewModel
Build an @Observable view model for a screen.
Designing a ViewModel is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A ViewModel Is Just a Class
A ViewModel is usually a class that holds the state and logic for one screen. Using a class lets several views share the same live instance.
class ProfileViewModel {
var name: String = ""
}Make It Observable
Add the @Observable macro so SwiftUI tracks property changes and re-renders the view automatically when state updates.
@Observable
class ProfileViewModel {
var name = ""
}Expose View-Ready State
Give the ViewModel properties the view can read directly, like a formatted greeting, so the view never does its own string building.
var greeting: String {
"Hi, " + name
}Hold the Loading Flag
Track UI status inside the ViewModel. A simple isLoading boolean lets the view decide when to show a spinner versus content.
var isLoading = falseKeep Intent in Methods
Expose actions as methods like load() or save(). The view calls them on tap; the ViewModel decides exactly what happens.
func load() {
isLoading = true
}Name Properties for the UI
Name state after what the screen shows, like buttonTitle or rows, not after raw data. This keeps the view almost declarative.
Hide the Messy Details
Parsing, math, and edge cases belong inside the ViewModel. The view should see clean values and never touch the rough work.
One ViewModel per Screen
Give each screen its own focused ViewModel. A small, single-purpose model is far easier to read, reason about, and test.
Start Empty, Then Fill
Initialize the ViewModel with sensible empty defaults, then populate it when data arrives. The view shows a calm empty state meanwhile.
Avoid View Code Inside
Never import view types or layout code into a ViewModel. Keeping it UI-free is what makes it portable and easy to unit test. ✨
A Complete Small Example
This tiny ViewModel exposes a count and a method to change it, giving the view everything it needs in two clear members.
@Observable
class CounterViewModel {
var count = 0
func increment() { count += 1 }
}Quick Check
Which macro makes a ViewModel class trigger SwiftUI updates?
Recap: Designing a ViewModel
You built an @Observable class that exposes view-ready state and intent methods while hiding the messy details. Next you will bind views to it.
Frequently asked questions
Is the “Designing a ViewModel” lesson free?
Yes — the full text of “Designing a ViewModel” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Designing a ViewModel”?
Build an @Observable view model for a screen. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Designing a ViewModel” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why MVVM Fits SwiftUI
- Designing a ViewModel
- Binding Views to ViewModels
- Dependency Injection for ViewModels