SwiftUI Academy · บทเรียน

การเพิ่ม การอัปเดต และการลบ

เปลี่ยนแปลงที่จัดเก็บและสะท้อนการเปลี่ยนแปลงแบบทันที

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

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

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

Changing Your Data

Now you will change the store: add new records, edit existing ones, and remove what you no longer need. ✏️

Inserting a Record

Create an object and call insert on the context to add it. SwiftData starts tracking it for saving right away.

let task = Task(title: "Walk dog")
context.insert(task)

Insert Then Appears

After an insert, any @Query view shows the new row at once. You do not refresh the list yourself.

Updating a Property

To edit a record, just change its property on the tracked object. SwiftData notices and saves the new value.

task.isDone = true

No Special Edit Call

There is no separate update method. Because the object is tracked, direct assignment is the whole update flow.

task.title = "Walk the dog twice"

Deleting a Record

Remove an object with delete on the context. It disappears from the store and from every query view.

context.delete(task)

Deleting from a List

Wire up swipe-to-delete with onDelete, then call delete for each index. Users remove rows with a familiar gesture.

.onDelete { offsets in
    for i in offsets {
        context.delete(tasks[i])
    }
}

Saving the Changes

SwiftData autosaves, but you can call save to commit immediately. It is wise before the app might close.

try context.save()

Batch Operations

Loop over a set of objects to insert or delete many at once. The context batches the work efficiently for you.

for t in finished {
    context.delete(t)
}

Handling Errors

Wrap a manual save in do-catch so a failure does not crash your app. You can warn the user instead.

do { try context.save() }
catch { print(error) }

Changes Flow to the UI

Every insert, edit, and delete reflects live in your views. The store and screen stay perfectly in step.

Quick Check

Quick check on editing data.

Recap: Editing Data

You can now insert, edit by assignment, and delete records, with the UI updating live. You have full control over your SwiftData store. 🚀

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

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

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

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

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

บทเรียน “การเพิ่ม การอัปเดต และการลบ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การเพิ่ม การอัปเดต และการลบ”

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

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

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

บทเรียน “การเพิ่ม การอัปเดต และการลบ” ใช้เวลานานแค่ไหน

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

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

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

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

  1. กำหนดชนิด @Model
  2. คอนเทนเนอร์และบริบทของโมเดล
  3. ค้นหาด้วย @Query
  4. การเพิ่ม การอัปเดต และการลบ
← กลับไปที่ SwiftUI Academy