最近 Swift 有一个新的方法叫做 move for Arrays。我们可以使用它来配合 Swiftui 列表拖放列表移动。我的挑战是我不明白它是如何独立工作的,Apple 中的文档对此很少见。这是我在使用此功能时注意到的一种奇怪行为。
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)")
结果是:
after 2 -> 1 ["a", "c", "b", "d"]
after 1 -> 2 ["a", "b", "c", "d"]
after 2 -> 2 ["a", "b", "c", "d"]
我对 1->2 结果感到非常困惑……那怎么不改变数组!它应该与 2->1 相同,不是吗?
请帮我理解。