Mathias Verhoeven
Mathias Verhoeven

Reputation: 1067

Checking if text fields are empty cause error in Swift 2

I am trying to check if a textbox has no value.

When I do this:

if(userEmail?.isEmpty || userPassword?.isEmpty || userPasswordRepeat?.isEmpty)  

I get the following error enter image description here

I tried adding "?" before the ".isEmpty" but the error won't go away enter image description here

Any ideas?

Upvotes: 0

Views: 1212

Answers (2)

Francesco Vadicamo
Francesco Vadicamo

Reputation: 5542

If interested also in positive case, the following is an alternative solution for Swift 2:

let email = self.txtEmail.text where !email.isEmpty, let password = self.txtPassword.text where !password.isEmpty {
    //all fields are not nil && not empty
}else{
    //some field is nil or empty
}

Upvotes: 0

Bhavin Bhadani
Bhavin Bhadani

Reputation: 22374

Try this....

if txtEmail.text?.isEmpty == true || txtPassword.text?.isEmpty == true || txtRePassword.text?.isEmpty == true{
        print("true")
}

Upvotes: 1

Related Questions