Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

392
Views
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
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!