0

I am try to check textfield is blank or not and what I am trying is

if(EmailTextField.text.characters.count>0)
{
}  

but I can't use relation operator on this count method. Why?

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78

2 Answers2

3

You can use the String's isEmpty property:

//if EmailTextField is a UITextField instance 
if EmailTextField.text != nil && !EmailTextField.text!.isEmpty {

}

if let text = EmailTextField.text where !text.isEmpty {

}
Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
3

By extension... To ensure that its not even take any blank spaces...

extension String {

var isBlank: Bool {
       get {
            let trimmed = stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
            return trimmed.isEmpty
        }
   }
}

Usage:

if !EmailTextField.text!.isBlank{
   //TextField is not blank
}
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108