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

428
Views
Swift 5: índice de un carácter en cadena

Antes de Swift 5, tenía esta extensión funcionando:

 fileprivate extension String { func indexOf(char: Character) -> Int? { return firstIndex(of: char)?.encodedOffset } }

Ahora, recibo un mensaje obsoleto:

 'encodedOffset' is deprecated: encodedOffset has been deprecated as most common usage is incorrect. Use `utf16Offset(in:)` to achieve the same behavior.

¿Existe una solución más simple para esto en lugar de usar utf16Offset(in:) ?

Solo necesito el índice de la posición del carácter pasado como un Int.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Después de algún tiempo tengo que admitir que mi respuesta original era incorrecta.

En Swift hay dos métodos: firstIndex(of:) y lastIndex(of:)

Ambos devuelven Int? representando el índice del primer/último elemento en Array que es igual al elemento pasado (si hay alguno, de lo contrario devuelve nil ).

Por lo tanto, debe evitar usar su método personalizado para obtener el índice porque podría haber dos elementos iguales y no sabría qué índice necesita. Así que trate de pensar en su uso y decida qué índice es más adecuado para usted; primero o último. 🙏


Respuesta original:

¿Y qué tiene de malo utf16Offset(in:) ? Este es el camino a seguir con Swift 5

 fileprivate extension String { func indexOf(char: Character) -> Int? { return firstIndex(of: char)?.utf16Offset(in: self) } }
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!