@Environmentでモデルを共有する
ビューの環境にモデルを注入します。
「@Environmentでモデルを共有する」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Sharing Down Many Levels
Passing a model through every view in between gets tedious. The environment lets deep child views reach it directly.
What the Environment Is
The environment is a shared bag of values flowing down the view tree, so any descendant can read what an ancestor placed there.
Injecting with .environment
Place an observable model into the tree with the .environment modifier on a parent view.
ContentView()
.environment(model)Reading with @Environment
A child reads the shared model by declaring a property with the @Environment wrapper and the model type.
@Environment(Counter.self) private var modelMatching by Type
The environment finds the model by its type, so the type you inject must match the type a child asks for.
No Manual Passing
Children no longer need the model handed through every initializer. They simply pull it from the environment when needed.
Using the Shared Model
Once read, the environment model behaves like any other: read its values and call its methods.
Text("\(model.count)")
Button("Add") { model.increment() }One Instance, Many Readers
Everyone reads the same injected instance, so changes made in one screen show up everywhere that reads it.
Inject Once, High Up
Inject the model near the top of the tree, often at the app root, so every screen below can share it.
WindowGroup {
RootView().environment(model)
}Owner Still Uses @State
One view still owns the model with @State and injects it. The environment only shares that single instance downward.
Missing Model Crashes
If a child reads a model the environment never received, the app crashes, so always inject before any reader appears.
Quick Check
Final check on sharing through the environment.
Recap
You shared one model across the view tree using @Environment: inject it high up, then read it by type in any descendant. 🌍
よくある質問
「@Environmentでモデルを共有する」レッスンは無料ですか?
はい。「@Environmentでモデルを共有する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「@Environmentでモデルを共有する」で何を学びますか?
ビューの環境にモデルを注入します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「@Environmentでモデルを共有する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ビューの状態からモデルクラスへ
- @Observableマクロ
- 所有するオブジェクトに@Stateを使う
- @Environmentでモデルを共有する