So I understand that the following code works:
struct ContentView: View {
func removeRows(at offsets: IndexSet) {
numbers.remove(atOffsets: offsets)
}
@State private var numbers = [Int]()
@State private var currentNumber = 1
var body: some View {
VStack {
List {
ForEach(numbers, id: \.self) {
Text("\($0)")
}
.onDelete(perform: removeRows)
}
Button("Add Number") {
self.numbers.append(self.currentNumber)
self.currentNumber += 1
}
}
}
}
What I don't understand is how the removeRows(at offsets: IndexSet) custom function handler for .onDelete() works. Specifically, why is there an argument label at before offsets:. Could you use on instead of at for example? Is the argument labels just for us to better understand the code?
Then there's IndexSet which I think I understand as a type that holds an Int for indexing purposes or ranges of Ints for use in deleting a row in the list, so that swiftui knows which row to delete?
I think I understand numbers.remove(atOffsets: offsets) is used to actually do the removing of the list row at offsets (in terms of the actual row index being deleted) - but i'm not sure how offsets is actually known by swift? What actually is offsets and how is it's value set?
I'm trying to learn by following this. I still have a very basic knowledge of swiftui, so please explain like I am super noob at this.