更新:从
Swift 3.2/4(Xcode 9)开始,您必须在集合上使用
swapAt()
方法。
scatola.swapAt(fromIndexPath.row, toIndexPath.row)
因为将数组作为两个不同的inout
参数传递给同一个函数现在已经不再合法,请比较SE-0173 添加MutableCollection.swapAt(_:_:)
。
更新:我使用 Xcode 6.4 再次测试了该代码,问题已经不存在了。 它可以按预期编译和运行。
(旧回答:) 我假设 scatola
是视图控制器中的存储属性:
var scatola : [Int] = []
您的问题似乎与 https://devforums.apple.com/thread/240425 中讨论的问题有关。这个问题已经能够被复制:
class MyClass {
var array = [1, 2, 3]
func foo() {
swap(&array[0], &array[1])
}
}
编译器输出:
错误:在多个参数中调用计算属性 'array' 的 inout 写回出现,引入了无效的别名
swap(&array[0], &array[1])
^~~~~~~~
注意:并发写回发生在这里
swap(&array[0], &array[1])
^~~~~~~~
我还没有完全掌握讨论的内容(这里太晚了:), 但是有一个提出的“解决方法”,即将该属性标记为 final(以便在子类中无法覆盖它):
final var scatola : [Int] = []
我发现的另一个解决方法是获取底层数组存储的指针:
scatola.withUnsafeMutableBufferPointer { (inout ptr:UnsafeMutableBufferPointer<Int>) -> Void in
swap(&ptr[fromIndexPath.row], &ptr[toIndexPath.row])
}
当然,万无一失的解决方案就是
let tmp = scatola[fromIndexPath.row]
scatola[fromIndexPath.row] = scatola[toIndexPath.row]
scatola[toIndexPath.row] = tmp
scatola
的声明。 - rob mayoff