首页 > 解决方案 > Simultaneous accesses to

, but modification requires exclusive access

问题描述

I want to specify the property which is an array for manipulation in the function myFunc, but I am getting this error. Here is my code sketch.

self.data = MyObject()

func myFunc(x: inout [Int], y: inout [[Int]]) {
    //manipulation code to x and y
}

myFunc(x: &self.data.array1, y: &self.data.array2)
myFunc(x: &self.data.array3, y: &self.data.array4)

Any idea how to make it work? Is there a better pattern I should use for this use case? Thx for advance!

标签: swift4shared-memory

解决方案


在 Swift 文档中进行了非常彻底的解释:

https://docs.swift.org/swift-book/LanguageGuide/MemorySafety.html

基本上,您威胁要同时以两种不同的方式对同一个对象进行变异/写入。这是不连贯的,如果编译器不阻止你运行时会。


推荐阅读