0Pricing
SwiftUI Academy · Lesson

Defining @Model Types

Annotate classes as persistent models.

Defining @Model Types is a free SwiftUI Academy lesson on CoddyKit — lesson 1 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.

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: Int

Optional 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: UUID

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

Quick 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. 🎉

Frequently asked questions

Is the “Defining @Model Types” lesson free?

Yes — the full text of “Defining @Model Types” 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 “Defining @Model Types”?

Annotate classes as persistent models. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Defining @Model Types” 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

  1. Defining @Model Types
  2. The Model Container & Context
  3. Querying with @Query
  4. Inserting, Updating & Deleting
← Back to SwiftUI Academy