近期Swift新增了一种名为move的数组方法,我们可以将其与SwiftUI列表拖放功能相结合。我目前遇到的挑战是,我不理解它如何独立使用,而且苹果公司的文档对此有所欠缺。以下是我在玩弄此函数时注意到的一种奇怪行为。
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相同,不是吗?
请帮我理解。