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

203
Views
Permitir retroceso con juego de caracteres

Tengo problemas para borrar en mi campo de texto. Así que tengo un campo de texto para el nombre de una persona que solo permite letras. Pero cuando presiono eliminar o retroceder, parece que no funciona. Así es como se ve mi código.

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let set = CharacterSet.letter return (string.rangeOfCharacter(from: set) != nil) }

No estoy seguro de por qué la función de retroceso/eliminación no funciona.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Cuando el usuario toca el retroceso, la string será la cadena vacía. Por lo tanto, rangeOfCharacter será nil , por lo que su código devuelve false y evita que funcione el retroceso.

Prueba esto:

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { return string.isEmpty || string.rangeOfCharacter(from: CharacterSet.letter) != nil }
over 4 years ago · Santiago Trujillo Report

0

Cuando toque la tecla de retroceso, verifique el código de retroceso y devuelva falso si rangeOfCharacter para su expresión regular es nil ; de lo contrario, devuelva verdadero.

Prueba esto:

 let char = text.cString(using: String.Encoding.utf8)! let isBackSpace = strcmp(char, "\\b") if (isBackSpace == -92) { }else if text.range(of: kRangeWithoutSpace, options:.regularExpression) == nil { return false }
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!