@State กับ @Binding
เลือกการเป็นเจ้าของที่เหมาะสมสำหรับคุณสมบัติแต่ละรายการ
@State กับ @Binding เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Two Wrappers, One Goal
Both @State and @Binding manage mutable data, but they play different roles. Picking the right one keeps ownership clear. 🎯
@State Owns
Use @State when this view is the source of truth. It creates and stores the value for itself.
@State private var count = 0@Binding Borrows
Use @Binding when the value lives somewhere else and this view only needs to read and write it.
@Binding var count: IntWho Creates the Value
@State initializes a value. @Binding never does; it always receives one from a parent that owns it.
Mark State Private
@State is view-local, so mark it private. No one outside should reach in and set it directly.
@State private var isOn = falseBindings Stay Open
A @Binding is not private; the parent must be able to pass a value in when it creates the child.
The Dollar Sign Link
An owners @State turns into a child @Binding through the dollar sign. That is the bridge between the two.
ChildView(count: $count)Top of the Tree
The view highest up that truly owns the data uses @State. Everything below it borrows with @Binding.
A Simple Rule
Ask: do I create this value? If yes, @State. If a parent hands it to me, @Binding. That choice rarely fails you.
Wrong Choice, Real Bugs
Use @State where you meant @Binding and the child edits a copy. The parent never sees the change, and the UI quietly desyncs.
Same Value, Two Roles
One piece of data is @State in its owner and @Binding in every borrower. Same value, different responsibilities. 🔄
Quick Check
Quick check on choosing wrappers.
Recap: Own or Borrow
Use @State to own a value and @Binding to borrow one. Match the wrapper to who creates the data and your views stay in sync. 🧠
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “@State กับ @Binding” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “@State กับ @Binding” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “@State กับ @Binding”
เลือกการเป็นเจ้าของที่เหมาะสมสำหรับคุณสมบัติแต่ละรายการ คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “@State กับ @Binding” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดมุมมองลูกจึงต้องใช้ @Binding
- ส่งการเชื่อมโยงข้อมูลลงไป
- สร้างแถวสวิตช์ที่นำกลับมาใช้ใหม่ได้
- @State กับ @Binding