zs2020
zs2020

Reputation: 54543

Simultaneous accesses to <Address>, 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!

Upvotes: 5

Views: 6630

Answers (1)

matt
matt

Reputation: 535890

Very thoroughly explained in the Swift documentation:

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/memorysafety/

Basically you are threatening to mutate / write to the same object in two different ways simultaneously. That’s incoherent and if the compiler doesn’t stop you the runtime will.

Upvotes: 4

Related Questions