How to fix "Overlapping accesses to 'self', but modification requires exclusive access; consider copying to a local variable" error in my code?

I can't continue with my app and I can't test run it because something is wrong in my code that I don't know how to fix! Here is the code:

import Foundation

extension Array {
    mutating func shuffle() {
        if count < 2 { return }
        for i in 0..<(count - 1) {
            let j = Int(arc4random_uniform(UInt32(count - i))) + i 
            customSwap(a: &self[i], b: &self[j])
        }
    }
}

func customSwap<T>(a: inout T, b: inout T) {
    let temp = a
    a = b
    b = temp
}

Upvotes: 5

Views: 2890

Answers (1)

vacawama
vacawama

Reputation: 154691

The problem is that an array is a value type, and when you modify one element you change the whole array. So your call to customSwap() is passing in two references to the whole array which leads to the overlapping accesses to self error.

Instead, you could write customSwap() to take one copy of the array and the indices you want to swap:

func customSwap<T>(_ array: inout [T], _ a: Int, _ b: Int) {
    let temp = array[a]
    array[a] = array[b]
    array[b] = temp
}

and then call it like this:

customSwap(&self, i, j)

But you don't have to do that, because Array has a built-in swapAt(_:_) defined like this:

 mutating func swapAt(_ i: Int, _ j: Int)

So you could replace your customSwap call with:

self.swapAt(i, j)

But Array has a built-in shuffle() that you can just call instead of implementing it yourself.

Upvotes: 7

Related Questions