Featured blog image
IOS β€’ 5 min read

πŸ“ Day 3 β€” Adding and Deleting Items with SwiftData

Author

Arjun

IOS Developer

Yesterday, I built a simple list of saved wishes using SwiftData.
But the list was static β€” I couldn’t add or remove anything.

Today, I learned how to insert new data and delete existing data using SwiftData and SwiftUI.
It turned out to be super easy!


βž• Adding New Items

I added a β€œ+” button in the navigation bar.
When tapped, it shows an alert with a text field to enter a new wish.
Then I insert it into the database using viewContext.insert().

@State private var isAlertShowing = false
@State private var title = ""

.toolbar {
   ToolbarItem(placement: .navigationBarTrailing) {
       Button {
           isAlertShowing.toggle()
       } label: {
           Image(systemName: "plus")
               .imageScale(.large)
       }
   }
}
.alert("Add Wish Item", isPresented: $isAlertShowing) {
   TextField("Enter Wish", text: $title)
   
   Button("Save") {
       viewContext.insert(Wish(name: title))
       title = ""
   }
}
πŸ“ Notes

  • viewContext comes from @Environment(\.modelContext)

  • Once inserted, the @Query property updates automatically β€” no manual reload needed


πŸ—‘ Deleting Items with Swipe Actions

SwiftUI makes deleting really simple.
I just added .swipeActions on each row:
ForEach(items) { item in
   Text(item.name)
       .font(.subheadline)
       .padding(.vertical, 8)
       .swipeActions {
           Button("Delete", role: .destructive) {
               viewContext.delete(item)
           }
       }
}
As soon as you delete it from the viewContext, the UI list updates instantly.


⚑ Showing a Count in Toolbar

Just for fun, I added a bottom bar to show how many wishes are saved:

if !items.isEmpty {
   ToolbarItem(placement: .bottomBar) {
       Text("\(items.count) wish(es)")
   }
}


πŸ“Œ Full Working Example

 
struct SwiftDataView: View {   @Environment(\.modelContext) private var viewContext   @Query var items: [Wish]   @State private var isAlertShowing = false   @State private var title = ""        var body: some View {        NavigationStack {            List {                ForEach(items) { item in                    Text(item.name)                        .font(.subheadline)                        .padding(.vertical, 8)                        .swipeActions {                            Button("Delete", role: .destructive) {                                viewContext.delete(item)                            }                        }                }            }            .navigationTitle("Wishes")            .overlay {                if items.isEmpty {                    ContentUnavailableView("No wish add Yet!", systemImage: "heart.fill")                }            }            .toolbar {                ToolbarItem(placement: .navigationBarTrailing) {                    Button {                        isAlertShowing.toggle()                    } label: {                        Image(systemName: "plus")                            .imageScale(.large)                    }                }                if !items.isEmpty {                    ToolbarItem(placement: .bottomBar) {                        Text("\(items.count) wish(es)")                    }                }            }            .alert("Add Wish Item", isPresented: $isAlertShowing) {                TextField("Enter Wish", text: $title)                Button("Save") {                    viewContext.insert(Wish(name: title))                    title = ""                }            }        }    } }

✨ What I Learned Today

  • How to insert data into SwiftData

  • How to delete data from SwiftData

  • How @Query automatically refreshes the list

  • How to combine SwiftUI toolbar, alerts, and swipe actions

Now my app finally feels alive β€” I can add wishes, remove them, and see the count update instantly. πŸš€
 

 


 

Related Topics

Arjun

IOS Developer
Passionate iOS Developer with experience building intuitive and high-performance apps using Swift and SwiftUI. Skilled in designing clean architectures, integrating APIs, and optimizing user experience. Constantly exploring new Apple technologies to deliver impactful mobile solutions.