Я пытаюсь выяснить, как удалить из массива в swiftui для Ма c. Все статьи, которые я могу найти, показывают, как это сделать способом UIKit с помощью метода .ondelete, добавленного в foreach. Это не работает для Ma c, потому что у меня нет красной кнопки удаления, как iOS. Итак, как мне удалить элементы из массива в ForEach на Ma c. вот код, который я пробовал, который дает мне ошибку
Fatal error: Index out of range: file
import SwiftUI
struct ContentView: View {
@State var splitItems:[SplitItem] = [
SplitItem(id: 0, name: "Item#1"),
SplitItem(id: 1, name: "Item#2")
]
var body: some View {
VStack {
Text("Value: \(self.splitItems[0].name)")
ForEach(self.splitItems.indices, id:\.self) { index in
HStack {
TextField("Item# ", text: self.$splitItems[index].name)
Button(action: {self.removeItem(index: index)}) {Text("-")}
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
func removeItem(index:Int) {
self.splitItems.remove(at: index)
}
}
struct SplitItem:Identifiable {
var id:Int
var name:String
}