SwiftUI Academy · บทเรียน

ใช้โมเดลร่วมกันผ่าน @Environment

ฉีดโมเดลเข้าไปในสภาพแวดล้อมของมุมมอง

บทเรียน 4 จาก 413 ขั้นตอน

ใช้โมเดลร่วมกันผ่าน @Environment เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 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. 🌍

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “ใช้โมเดลร่วมกันผ่าน @Environment” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ใช้โมเดลร่วมกันผ่าน @Environment” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ใช้โมเดลร่วมกันผ่าน @Environment”

ฉีดโมเดลเข้าไปในสภาพแวดล้อมของมุมมอง คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ใช้โมเดลร่วมกันผ่าน @Environment” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. จากสถานะมุมมองสู่คลาสโมเดล
  2. มาโคร @Observable
  3. @State สำหรับออบเจ็กต์ที่เป็นเจ้าของ
  4. ใช้โมเดลร่วมกันผ่าน @Environment
← กลับไปที่ SwiftUI Academy