Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

388
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda