Swift Data for iOS is a concise, runnable learning example that shows how to use Apple's SwiftData framework together with SwiftUI. It demonstrates the core persistence loop: defining a @Model class, wiring up a ModelContainer, querying stored records with @Query, and inserting or deleting items through the modelContext. The app itself is a single screen where you type text, press return to save it, and swipe to delete — all persisted on-device across launches.
- Declaring a persistent model with the
@Modelmacro (SwiftDataModel). - Bootstrapping a
ModelContainerat the app entry point and injecting it with.modelContainer(...). - Reading persisted records reactively via the
@Queryproperty wrapper. - Inserting new records into the store using
@Environment(\.modelContext)andcontext.insert(...). - Deleting records with
context.delete(...)wired to SwiftUI's.onDeleteswipe action. - Using
#Previewwith an in-memory model container for fast SwiftUI previews.
git clone https://github.com/ahmetbostanciklioglu/Swift-Data.git
cd Swift-Data
open "Swift Data for iOS.xcodeproj"Then select an iOS 18 simulator (or a connected device) and press ⌘R to build and run.
- iOS 18.0 or later
- Xcode 16 or later
- Swift 5.0
Ahmet Bostancıklıoğlu — @ahmetbostanciklioglu · ahmetbostancikli@gmail.com
⭐ If this helped you, consider giving the repo a star!