0Pricing
SwiftUI Academy · Lekcja

Projektowanie ViewModelu

Utwórz ViewModel ekranu z użyciem @Observable

Projektowanie ViewModelu to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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 = false

Keep 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.

Często zadawane pytania

Czy lekcja „Projektowanie ViewModelu” jest bezpłatna?

Tak — pełny tekst „Projektowanie ViewModelu” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Projektowanie ViewModelu”?

Utwórz ViewModel ekranu z użyciem @Observable Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?

Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Projektowanie ViewModelu”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?

Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dlaczego MVVM pasuje do SwiftUI
  2. Projektowanie ViewModelu
  3. Powiązanie widoków z ViewModelami
  4. Wstrzykiwanie zależności do ViewModeli
← Powrót do SwiftUI Academy