Ein ViewModel entwerfen
Erstellen Sie ein @Observable-ViewModel für einen Bildschirm.
Ein ViewModel entwerfen ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Ein ViewModel entwerfen“ kostenlos?
Ja — der vollständige Text von „Ein ViewModel entwerfen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Ein ViewModel entwerfen“?
Erstellen Sie ein @Observable-ViewModel für einen Bildschirm. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um SwiftUI Academy zu starten?
Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Ein ViewModel entwerfen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?
Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Warum MVVM zu SwiftUI passt
- Ein ViewModel entwerfen
- Ansichten an ViewModels binden
- Dependency Injection für ViewModels