-3

I would like to check in my app, if there is already a - character in the textField.

How can I do this?

Kerberos
  • 4,036
  • 3
  • 36
  • 55
  • Need more explanation of your question? Actually where do you want to check when keyboard shows or where ? – Rizwan Aug 08 '18 at 09:40

2 Answers2

2

You can use contains() method

let myString = "test"
if myString.contains("e"){
   let index = myString.index(of: "e")
   myString.remove(at: index!)
}

p.s you can get myString from textField.text

Enea Dume
  • 3,014
  • 3
  • 21
  • 36
0

You can simply:

var yourTextFieldString: String = textField.text

yourTextFieldString.contains("-")

It returns a Bool.

Kerberos
  • 4,036
  • 3
  • 36
  • 55