SwiftUI Academy · Lektion

Models über @Environment teilen

Fügen Sie ein Model in die Ansichts-Umgebung ein.

Lektion 4 von 413 Schritte

Models über @Environment teilen ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 model

Matching 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. 🌍

Kostenlos starten

Lerne Swift mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
30
Lektionen
120

Häufig gestellte Fragen

Ist die Lektion „Models über @Environment teilen“ kostenlos?

Ja — der vollständige Text von „Models über @Environment teilen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Models über @Environment teilen“?

Fügen Sie ein Model in die Ansichts-Umgebung ein. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um SwiftUI Academy zu starten?

Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Models über @Environment teilen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?

Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Vom Ansichts-State zu einer Model-Klasse
  2. Das @Observable-Makro
  3. @State für eigene Objekte
  4. Models über @Environment teilen
← Zurück zu SwiftUI Academy