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

405
Views
¿Cómo corregir el error "Accesos superpuestos a 'uno mismo', pero la modificación requiere acceso exclusivo; considere copiar a una variable local" en mi código?

¡No puedo continuar con mi aplicación y no puedo probarla porque hay algo mal en mi código que no sé cómo arreglar! Aquí está el código:

 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

El problema es que una matriz es un tipo de valor, y cuando modifica un elemento, cambia toda la matriz. Entonces, su llamada a customSwap() está pasando dos referencias a toda la matriz, lo que conduce a los accesos superpuestos al error propio .

En su lugar, podría escribir customSwap() para tomar una copia de la matriz y los índices que desea intercambiar:

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

y luego llamarlo así:

 customSwap(&self, i, j)

Pero no tienes que hacer eso, porque Array tiene un swapAt(_:_) definido así:

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

Entonces podría reemplazar su llamada customSwap con:

 self.swapAt(i, j)

Pero Array tiene un shuffle() incorporado al que puede llamar en lugar de implementarlo usted mismo.

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!