0Pricing
SwiftUI Academy · レッスン

所有するオブジェクトに@Stateを使う

@Stateで監視可能なモデルを所有します。

「所有するオブジェクトに@Stateを使う」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Who Owns the Model

An observable model needs an owner: a view that creates it and keeps it alive for as long as the screen exists.

@State Owns the Instance

Use @State to let a view own an observable model. The view creates it once and holds onto it across redraws.

@State private var model = Counter()

Why @State Here

Without @State, a fresh model would be made on every redraw, wiping your data. @State persists the same instance.

Reading the Model

Read tracked properties straight from the model in your body, and the view refreshes when they change.

Text("Count: \(model.count)")

Calling Model Methods

Trigger behaviour by calling the model methods from your controls, keeping the view free of the actual logic.

Button("Add") { model.increment() }

Mark It private

Keep owned state private so other types cannot reach in and mutate the model behind the view back.

@State private var model = Counter()

No $ to Read

To read an observable model you use it directly. The dollar sign $ is only for making a binding, which we cover later.

A Full Owned View

Here a view owns the model, reads its value, and updates it on tap, all in one tidy place.

struct CounterView: View {
    @State private var model = Counter()
    var body: some View {
        Button("\(model.count)") { model.increment() }
    }
}

Survives Redraws

Because @State is managed by SwiftUI, your model survives every body re-evaluation while the view is on screen.

One Owner Rule

A model should have a single owner. Child views receive access instead of each making their own copy.

Lifetime Tied to the View

The model lives as long as its owning view. When the view leaves the screen for good, the model is released.

Quick Check

Let us confirm how ownership works.

Recap

You used @State to let a view own its observable model, so a single instance survives redraws while the view reads and updates it. ✨

よくある質問

「所有するオブジェクトに@Stateを使う」レッスンは無料ですか?

はい。「所有するオブジェクトに@Stateを使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「所有するオブジェクトに@Stateを使う」で何を学びますか?

@Stateで監視可能なモデルを所有します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「所有するオブジェクトに@Stateを使う」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ビューの状態からモデルクラスへ
  2. @Observableマクロ
  3. 所有するオブジェクトに@Stateを使う
  4. @Environmentでモデルを共有する
← SwiftUI Academyに戻る