Identifiable และ ID ที่เสถียร
ทำให้แบบจำลองเป็น Identifiable เพื่อให้การเปรียบเทียบความแตกต่างทำงานถูกต้อง
Identifiable และ ID ที่เสถียร เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Diffing Problem
When data changes, SwiftUI must know which rows moved, stayed, or vanished. Stable identity makes that diffing correct. 🧩
Why id: .self Is Fragile
Using id: .self breaks when two items are equal or a value changes, because the identity shifts unexpectedly.
Meet Identifiable
The Identifiable protocol asks a type for just one thing: a stable, unique id property that never changes for that item.
protocol Identifiable {
var id: ID { get }
}Conforming a Model
Add an id property and conform to Identifiable, and your struct is ready for clean, reliable lists.
struct Task: Identifiable {
let id = UUID()
var title: String
}UUID for Fresh IDs
A UUID generates a guaranteed-unique value, perfect when items have no natural identifier of their own.
let id = UUID()ForEach Without id:
Once a type is Identifiable, ForEach finds the id by itself, so you can drop the id parameter entirely.
ForEach(tasks) { task in
Text(task.title)
}Stable Means Persistent
A good id never changes for the same logical item, even when its title or other fields are edited later on.
Natural Keys
If your data already has a unique field, like a database id, use it instead of generating a new one.
struct User: Identifiable {
let id: Int
var name: String
}Why It Matters for Animation
Correct identity lets SwiftUI animate moves and deletes smoothly instead of redrawing everything.
Avoid Index as ID
Using array indexes as ids is risky: inserting or deleting shifts every index and confuses diffing.
Identity Is About Sameness
Two items with the same id are treated as the same row; different ids mean different rows. Choose ids carefully.
Quick Check
Pick the most reliable approach to row identity.
Recap
You learned Identifiable: a stable id per item lets SwiftUI diff and animate lists correctly, far safer than id: .self. 🎉
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “Identifiable และ ID ที่เสถียร” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “Identifiable และ ID ที่เสถียร” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “Identifiable และ ID ที่เสถียร”
ทำให้แบบจำลองเป็น Identifiable เพื่อให้การเปรียบเทียบความแตกต่างทำงานถูกต้อง คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “Identifiable และ ID ที่เสถียร” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างรายการแบบคงที่
- การวนซ้ำด้วย ForEach
- Identifiable และ ID ที่เสถียร
- ส่วนและรูปแบบรายการ