我想从使用 a 显示为列表的数组中删除一个元素ForEach,但我还需要向 a 发送HTTP请求,REST API并且需要将元素的索引放入请求正文中。这是我的代码:
ForEach(self.symptoms, id: \.self) { symptom in
VStack(alignment: .leading) {
Text(symptom)
}
}.onDelete(perform: delete)
Run Code Online (Sandbox Code Playgroud)
这是删除功能:
func delete(at offsets: IndexSet) {
self.symptoms.remove(atOffsets: offsets)
// here I want to make the HTTP request
}
Run Code Online (Sandbox Code Playgroud)
如果您逐一删除,则以下内容为您提供已删除行的索引
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
}
Run Code Online (Sandbox Code Playgroud)