Recently Swift has a new method called move for Arrays. We can use it to tie in with Swiftui list drag and drop list moves. My challenge is that I don't understand how it works standalone and the docs in Apple are sparse about this. Here's a weird behavior I've noticed when I just play with this function.
var array = ["a", "b" ,"c", "d"]
print("before \(array)\n")
array = ["a", "b" ,"c", "d"]
array.move(fromOffsets: IndexSet(integer: 2), toOffset: 1)
print("after 2 -> 1 \(array)")
array = ["a", "b" ,"c", "d"]
array.move(fromOffsets: IndexSet(integer: 1), toOffset: 2)
print("after 1 -> 2 \(array)")
array = ["a", "b" ,"c", "d"]
array.move(fromOffsets: IndexSet(integer: 2), toOffset: 2)
print("after 2 -> 2 \(array)")
Results are:
after 2 -> 1 ["a", "c", "b", "d"]
after 1 -> 2 ["a", "b", "c", "d"]
after 2 -> 2 ["a", "b", "c", "d"]
I'm super confused about the 1->2 result ... how's that not change the array! It should be the same as the 2->1 shouldn't it?
Please help me understand.