挿入、更新、削除
ストアを変更し、その変化をリアルタイムに反映します。
「挿入、更新、削除」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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 = trueNo 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. 🚀
よくある質問
「挿入、更新、削除」レッスンは無料ですか?
はい。「挿入、更新、削除」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「挿入、更新、削除」で何を学びますか?
ストアを変更し、その変化をリアルタイムに反映します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「挿入、更新、削除」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。