Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

390
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda