How to check UITextField in Swift 2.0 is not empty? Is there a better way to count a string?

I am trying to verify that the text field is empty or not, and that I am trying to do

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

but I cannot use the relation operator for this count method. Why?

0
source share
2 answers

By extension ... To make sure it doesn't even accept spaces ...

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

Using:

 if !EmailTextField.text!.isBlank{ //TextField is not blank } 
+3
source

You can use the String isEmpty property:

 //if EmailTextField is a UITextField instance if EmailTextField.text != nil && !EmailTextField.text!.isEmpty { } if let text = EmailTextField.text where !text.isEmpty { } 
+3
source

Source: https://habr.com/ru/post/1258144/


All Articles