0Pricing
SwiftUI Academy · Lesson

From View State to a Model Class

Extract shared data into a class.

From View State to a Model Class 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.

Outgrowing @State

When a screen has many @State values, your view starts juggling data and UI at once. That mix gets messy fast as the screen grows. 🤔

What a Model Holds

A model is a plain object that holds your data and the rules around it, kept separate from how things look on screen.

A Simple Model Class

Start by moving related values into one class so they live together in a single, tidy place.

class Counter {
    var count = 0
}

Methods Carry the Logic

Put behaviour next to the data with methods, so the view just asks the model to act instead of doing the work itself.

class Counter {
    var count = 0
    func increment() { count += 1 }
}

Why a Class, Not a Struct

A class is a reference type, so every view sharing the same instance sees the exact same data, not its own private copy.

Views Stay Thin

With logic in the model, your view body shrinks to mostly layout. A thin view is far easier to read and change later.

One Source of Truth

Keeping data in the model gives you one source of truth, so screens never disagree about the current value.

Reusing the Model

Because the model is independent of any view, you can reuse it across several screens or even in tests.

Holding It in a View

A view can create and hold a model instance as a property, ready to read its values and call its methods.

struct CounterView: View {
    let model = Counter()
    var body: some View { Text("Count") }
}

The Missing Piece

So far the view can read the model, but it will not yet refresh when the data changes. We will fix that with observation next.

Separation of Concerns

This split is called separation of concerns: the view shows, the model decides. Each part has one clear job.

Quick Check

You moved your data into a class. Quick question about why.

Recap

You learned to lift data and logic out of a view into a model class, keeping views thin and giving your app one source of truth. ✨

Frequently asked questions

Is the “From View State to a Model Class” lesson free?

Yes — the full text of “From View State to a Model Class” 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 “From View State to a Model Class”?

Extract shared data into a class. 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 “From View State to a Model Class” 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. From View State to a Model Class
  2. The @Observable Macro
  3. @State for Owned Objects
  4. Sharing Models via @Environment
← Back to SwiftUI Academy