2

I'm new to swift and i have a ForEach list that displays information stored with CoreData. The swipe to delete method works .onDelete(perform: vm.deleteMemory) for each item shown on the list but i want a button to delete instead of swiping.

This is what i tried so far with my button action, but i get Cannot convert value of type 'IndexSet.Type' to expected argument type 'IndexSet' error.

    @StateObject var vm = CoreDataViewModel()
    VStack{
                Label {
                    Text("Here is a list of your memories")
                        .font(.system(size: 25))
                        .foregroundColor(.primary)
                } icon: {
                   Image(systemName: "hands.sparkles")
                    .font(.system(size: 25))
                }
                
                List {
                    ForEach(vm.savedEntites) { entity in
                        Text(entity.name ?? "No Name")
                            .font(.system(size: 25))
                        Button(action: { vm.deleteMemory(indexSet: IndexSet)}) {
                                    Label("Delete",systemImage: "trash")
                                }
                    }
                    .onDelete(perform: vm.deleteMemory) //swipe to delete
                   
                 
                }

Here is the delete function

 func deleteMemory(indexSet: IndexSet) {
         withAnimation {
            guard let index = indexSet.first else {return}
            let entity = savedEntites[index]
            container.viewContext.delete(entity)
             saveData()
         }
1
  • You need to pass along a indexSet in your deleteMemory function. So something like; .onDelete { offset in vm.deleteMemory(offset) } Also, your button action is using a function signature instead of calling a function. So you want to do something similar as the code above on that one. Commented Jun 23, 2021 at 11:29

1 Answer 1

3

You should change the signature of deleteMemory as the follwing:

   func deleteMemory(entity: Entity) {
     ----
   }

Entity is type of your entities.

And then call deleteMemory(entity: enttiy).

        Button(action: { withAnimation {
            vm.deleteMemory(entity: entity)
        } }) {
            Label("Delete",systemImage: "trash")
        }

Doing so you avoid retrieving Entity from saveList in deleteMemory.

    ForEach(vm.savedEntites) { entity in
        Text(entity.name ?? "No Name")
        .font(.system(size: 25))
        Button(action: { vm.deleteMemory(entity: entity)}) {
            Label("Delete",systemImage: "trash")
        }
    }

    func deleteMemory(entity: Entity) {
        withAnimation {
            container.viewContext.delete(entity)
            saveData()
        }
    }

However, I would not use withAnimation in deleteMemory.

    
    ForEach(vm.savedEntites) { entity in
        Text(entity.name ?? "No Name")
        .font(.system(size: 25))
        Button(action: { vm.deleteMemory(entity: entity)}) {
            Label("Delete",systemImage: "trash")
        }
    }
    func deleteMemory(entity: Entity) {
        container.viewContext.delete(entity)
        saveData()
    }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.