0Pricing
SwiftUI Academy · บทเรียน

มาโคร @Observable

ทำให้คลาสสังเกตการณ์ได้ เพื่อให้มุมมองติดตามการเปลี่ยนแปลง

มาโคร @Observable เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Problem to Solve

A plain model class holds data, but SwiftUI will not redraw when that data changes. We need to make the class observable.

Meet @Observable

The @Observable macro marks a class so SwiftUI watches its properties and refreshes any view that reads them. ✨

@Observable
class Counter {
    var count = 0
}

What a Macro Does

A macro generates extra code for you at compile time, so one short attribute adds all the change-tracking plumbing.

Importing Observation

The macro lives in the Observation framework, so add an import at the top of the file before you use it.

import Observation

@Observable
class Counter { var count = 0 }

No Wrappers Needed

With @Observable you mark plain stored properties. No per-property wrapper is required for SwiftUI to track them.

@Observable
class Counter {
    var count = 0
    var name = "Taps"
}

Reading Triggers Tracking

SwiftUI only refreshes for the properties a view actually reads in its body, so unused values cost you nothing.

Writing Triggers Updates

When you change a tracked property, every view that reads it is automatically scheduled to redraw.

model.count += 1  // views reading count refresh

Computed Properties Work Too

A computed property built from tracked values also updates views, since reading it reads its inputs.

@Observable
class Counter {
    var count = 0
    var label: String { "Taps: \(count)" }
}

Granular by Default

Tracking is granular: changing one property only refreshes views that read that property, not the whole screen.

Replacing ObservableObject

@Observable is the modern replacement for the older ObservableObject plus @Published pattern, with far less boilerplate.

Ignoring a Property

Mark a value with @ObservationIgnored when it should not trigger updates, like a cache or internal scratch value.

@Observable
class Counter {
    @ObservationIgnored var cache = 0
}

Quick Check

Time to check your grip on @Observable.

Recap

You made a model @Observable, so SwiftUI tracks the properties your views read and redraws them precisely when those values change. 🎉

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

บทเรียน “มาโคร @Observable” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “มาโคร @Observable”

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

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

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

บทเรียน “มาโคร @Observable” ใช้เวลานานแค่ไหน

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

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

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

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

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