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

209
Vistas
Allow Backspace with character set

I am having trouble deleting in my text field. So I have a text field for a person name only allowing letters. But when I hit the delete or backspace it doesn't seem to work. This is what my code looks like.

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

I am not sure why the backspace/delete is not working.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

When the user taps the backspace, string will be the empty string. So rangeOfCharacter will be nil so your code returns false preventing the backspace from working.

Try this:

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 Denunciar

0

When you tap the backspace check for backspace code and return false if rangeOfCharacter for your regular expression is nil else return true.

Try this:

        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 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