SwiftUI Academy · บทเรียน

การประกาศพร็อพเพอร์ตี @State

เพิ่ม @State เพื่อเก็บข้อมูลภายใน View ที่เปลี่ยนแปลงได้

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

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

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

Meet the @State Wrapper

You declare mutable view-local data by marking a property with @State. This property wrapper tells SwiftUI to store and watch the value for you. 🏷️

@State private var count = 0

Always Give It a Default

A @State property needs an initial value right where you declare it. SwiftUI uses it to create the storage the first time the view appears.

@State private var isOn = false

Mark It private

State belongs to one view, so declare it private. This signals that no outside code should reach in and set it directly, keeping ownership clear.

@State private var name = ""

Reading the Value

Read a @State property like any normal variable. Just use its name in body and SwiftUI tracks that this view depends on it.

Text("You tapped \(count) times")

Writing the Value

You can mutate a @State property even though the view is a struct. Assign to it from an action and SwiftUI schedules a redraw automatically.

Button("Add") { count += 1 }

It Persists Across Redraws

SwiftUI keeps the @State value alive across the many times the view body is recomputed. Your count won't reset just because the view redrew.

Putting It Together

Declare state, read it in the body, and mutate it from a button. That is the full loop that powers most simple interactions.

struct CounterView: View {
    @State private var count = 0
    var body: some View {
        Button("Count \(count)") { count += 1 }
    }
}

Use Simple Value Types

@State works best with value types like Int, Bool, String, or small structs. For shared objects you'll later reach for other tools, but values are the norm here.

One Owner Per Property

The view that declares @State is its owner. Other views can receive access, but only one view should hold the original state for a given value.

Don't Overuse It

Reach for @State only when a value truly changes while the view is on screen. Constant labels and passed-in data don't need it.

Naming for Clarity

Name state for what it represents, like isLoading or selectedTab. Clear names make the data flow obvious to anyone reading the view.

Quick Check

Which line correctly declares a private piece of view-local state?

Recap

Declare changing view data with @State, give it a default, and keep it private. Read it freely in body and mutate it from actions to trigger redraws. ✅

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

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

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

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

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

บทเรียน “การประกาศพร็อพเพอร์ตี @State” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การประกาศพร็อพเพอร์ตี @State”

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

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

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

บทเรียน “การประกาศพร็อพเพอร์ตี @State” ใช้เวลานานแค่ไหน

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

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

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

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

  1. เหตุใด View จึงต้องมีสถานะ
  2. การประกาศพร็อพเพอร์ตี @State
  3. การสร้างตัวนับการแตะ
  4. การสลับการมองเห็นด้วยสถานะ
← กลับไปที่ SwiftUI Academy