Definiowanie typów @Model
Oznaczaj klasy jako modele trwałe
Definiowanie typów @Model to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 1 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.
Meet SwiftData
SwiftData is Apple's modern way to save app data right on the device. You describe your data once, and it handles storage for you. 💾
The @Model Macro
You turn a plain class into a saved type by adding the @Model macro above it. That one line makes every instance persistable.
@Model
class Task {
}Stored Properties
Every stored property on a @Model class is saved automatically. No extra annotations are needed for basic values like text or numbers.
@Model
class Task {
var title: String
var isDone: Bool
}Models Are Classes
SwiftData models must be classes, not structs. SwiftData tracks them by reference so it can watch changes and save them.
Adding an Initializer
Give your model an init so you can create instances with starting values. This keeps your stored properties set when a task is made.
init(title: String, isDone: Bool = false) {
self.title = title
self.isDone = isDone
}Supported Property Types
SwiftData saves common types out of the box: String, Int, Double, Bool, Date, and Data all just work without setup.
var due: Date
var priority: IntOptional Properties
Mark a value optional when it may be missing. SwiftData stores nil happily, which is great for fields the user has not filled in yet.
var note: String?Unique Constraints
Add @Attribute(.unique) to stop duplicate values for a property. SwiftData will keep that field one-of-a-kind across your store.
@Attribute(.unique) var id: UUIDRelationships Between Models
Models can point to each other with a @Relationship. This links one record to another, like a project owning many tasks.
@Relationship var tasks: [Task]Cascade Deletes
A .cascade delete rule removes related records together. Delete a project and its tasks vanish too, keeping your data tidy.
@Relationship(deleteRule: .cascade)
var tasks: [Task]Ignoring a Property
Use @Transient for values you do not want saved, like a temporary cache. SwiftData skips these when writing to disk.
@Transient var isEditing = falseQuick Check
Quick check on defining models.
Recap: Defining Models
You learned that @Model turns a class into saved data, with normal properties, optionals, unique attributes, and relationships. Your data shape is ready. 🎉
Ucz się Swift dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 30
- Lekcje
- 120
Często zadawane pytania
Czy lekcja „Definiowanie typów @Model” jest bezpłatna?
Tak — pełny tekst „Definiowanie typów @Model” 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 „Definiowanie typów @Model”?
Oznaczaj klasy jako modele trwałe Ć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 1 z 4.
Ile czasu zajmuje lekcja „Definiowanie typów @Model”?
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
- Definiowanie typów @Model
- Kontener i kontekst modelu
- Wykonywanie zapytań za pomocą @Query
- Wstawianie, aktualizowanie i usuwanie