SwiftUI Academy · บทเรียน

@State สำหรับออบเจ็กต์ที่เป็นเจ้าของ

เป็นเจ้าของโมเดลที่สังเกตการณ์ได้ด้วย @State

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

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

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

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

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

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

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

บทเรียน “@State สำหรับออบเจ็กต์ที่เป็นเจ้าของ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “@State สำหรับออบเจ็กต์ที่เป็นเจ้าของ”

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

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

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

บทเรียน “@State สำหรับออบเจ็กต์ที่เป็นเจ้าของ” ใช้เวลานานแค่ไหน

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

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

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

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

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