The optional type "Bool" cannot be used as a boolean; Test instead of '! = Nil'

the optional type "Bool" cannot be used as a boolean; Test instead of '! = Nil' instead

enter image description here

At first I received an error if , replacing the condition if ( after ), the second, if the condition was never met. Any ideas?

Front

 if(userEmail?.isEmpty || userPassword?.isEmpty || userRepeatPassword?.isEmpty){ displayMyAlertMessage("All fields are required") return } if(userPassword != userRepeatPassword){ displayMyAlertMessage("Passwords do not match.") } 

After:

 if(userEmail != nil || userPassword != nil || userRepeatPassword != nil){ displayMyAlertMessage("All fields are required") return } if(userPassword != userRepeatPassword){ displayMyAlertMessage("Passwords do not match.") } 
+6
source share
4 answers

You check if the value is different from nil and whether it returns based on your comments and your second, if you probably want to check if it is.

 println("Checking login details") if(userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty){ displayMyAlertMessage("All fields are required") println("Fail to login not all fields where fill") return } else if(userPassword != userRepeatPassword){ displayMyAlertMessage("Passwords do not match.") println("Fail to login password does not match") } else { var uiAlert = UIAlertController(title: "Alert", message: "Registration was successful", preferredStyle: UIAlertControllerStyle.Alert) self.presentViewController(uiAlert, animated: true, completion: nil) uiAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in dismissViewControllerAnimated(true, completion:nil) })) } 
+3
source
 if(userEmail!.isEmpty || userPassword!.isEmpty || userRepeatPassword!.isEmpty) 

You cannot check the value of the optional, because perhaps you already know, being optional, it may or may not be there. One solution is called force unwrapping , and this is done with "!" ( Exclamation point). "?" (question mark) just lets the compiler know what may or may not be a value, so use "!" we will tell the compiler that we know what it may be, or it may not be the value inside this variable. But we know that there will be one, even if it is EMPTY STRING, unlike other programming languages โ€‹โ€‹that consider empty strings, or empty arrays of the "false" type. This is not so fast.

Your expression inside the conditional statement MUST be a valid boolean result.

+4
source

you need to wrap it ! instead of ? .
This will solve the error message:

 if (username!.isEmpty) ..... 
+3
source

With additional logical values, it works a little differently, you need to explicitly check if the value is nil. That is why your After: works, not your Before:

 //now this is checking if your values are nil (empty) rather than not nil //before if a user had valid fields, it would say that "All fields are required" //now, this will work if(userEmail == nil || userPassword == nil || userRepeatPassword == nil){ displayMyAlertMessage("All fields are required") } else if(userPassword != userRepeatPassword){ displayMyAlertMessage("Passwords do not match.") } else { //success //perform segue here to correct screen } 

There are several options for how you can execute segue, I will choose the presentViewController method, see below on how to integrate it. ...

 else { //success //perform segue here to correct screen presentViewController(yourMainScreenViewController, animated: true, completion: nil) } 

You can also use performSegueWithIdentifier ("yourMainScreenIdentifier", sender: zero) if you are not using the presentViewController method, for example:

 else { //success //perform segue here to correct screen performSegueWithIdentifier("yourMainScreenIdentifier", sender: nil) } 

I will add that I am assuming your displayMyAlertMessage :

 func displayMyAlertMessage(alert: String) { println(alert) } 
+1
source

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


All Articles