0

I am attempting to check if a text field is empty but am getting an error of "Type (Bool, Bool, Bool) does not conform protocol 'Boolean Type' "

 if(userEmail == "", userPassword == "", userRepeatPassword == "") {


        alertMessage("All fields are required")
        return

    }

I am using xcode 7

0

5 Answers 5

4

Try this,

if(userEmail == "" || userPassword == "" || userRepeatPassword == "") 
{
                //Do Something
}

(or)

if(userEmail == "" && userPassword == "" && userRepeatPassword == "") 
{
                //Do Something
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get a Thread1: Breakpoint 1.1 on the ' if(userEmail == "" || userPassword == "" || userRepeatPassword == "") { //Do Something }' what can I do to fix that?
Use this 'if(userEmail.text == "" || userPassword.text == "" || userRepeatPassword.text == "") { //Do Something }' (or) if you develop swift 2.0 then use this 'if(userEmail.text! == "" || userPassword.text! == "" || userRepeatPassword.text! == "") { //Do Something }'
0

Your should have used && as this:

let userEmail = "William"
let userPassword = "Totti"
let  userRepeatPassword = "Italy"


if(userEmail == "" && userPassword == "" &&  userRepeatPassword == "") {

    print("okay")

}

However, there is another way to do it and it is:

if(userEmail.isEmpty && userPassword.isEmpty && userRepeatPassword.isEmpty){
    print("okay")
}

Also another way is to check the number of characters like this:

if(userEmail.characters.count == 0 && userPassword.characters.count == 0 && userRepeatPassword.characters.count == 0){
    print("okay")
}

Comments

0
 if (userEmail?.isEmpty || userConfirmEmail?.isEmpty || userPassword?.isEmpty || userConfirmPassword?.isEmpty){

        alertMessage("All fields are required!");
        return;
    }

Use this method

1 Comment

Not a method on UITextField
0

This is the simplest way to check if a textfield is empty or not:

For Example:

if let userEmail = UserEmail.text, !userEmail.isEmpty,
let userPassword = UserPassword.text, !userPassword.isEmpty,
let userRepeatPassword = UserRepeatPassword.text, !userRepeatPassword.isEmpty { ... }

If all conditions are true then all variables are unwrapped.

Comments

0

After Swift 3.0, better to use the early binding statements, to make the code more clear.

guard !(userEmail.isEmpty)! && !(userPassword.isEmpty)! && !(userRepeatPassword.isEmpty)! else {
      return
}

// Do something if the fields contain text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.