10

I want to delete an element from an array that I am displaying as a list using a ForEach, but I also need to send a HTTP request to a REST API and I need to put the index of the element in the body of the request. Here is my code:

ForEach(self.symptoms, id: \.self) { symptom in
           VStack(alignment: .leading) {
                    Text(symptom)
            }
}.onDelete(perform: delete)

Here is the delete function:

func delete(at offsets: IndexSet) {     
      self.symptoms.remove(atOffsets: offsets)
      // here I want to make the HTTP request
}
NicolasElPapu
  • 1,612
  • 2
  • 11
  • 26
Susca Bogdan
  • 991
  • 1
  • 9
  • 23

1 Answers1

14

If you remove by-one, then the following give you index of deleted row

func delete(at offsets: IndexSet) {     
      self.symptoms.remove(atOffsets: offsets)

      // here I want to make the HTTP request
      let index = offsets[offsets.startIndex]

      // ... use index in HTTP request
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690