optional type 'Bool' cannot be used as a boolean; Test for '!=nil' instead
I got an error at first if, by replacing the if condition (after
), the second if condition did never run. Any idea?
if(userEmail?.isEmpty || userPassword?.isEmpty || userRepeatPassword?.isEmpty){
displayMyAlertMessage("All fields are required")
return
}
if(userPassword != userRepeatPassword){
displayMyAlertMessage("Passwords do not match.")
}
if(userEmail != nil || userPassword != nil || userRepeatPassword != nil){
displayMyAlertMessage("All fields are required")
return
}
if(userPassword != userRepeatPassword){
displayMyAlertMessage("Passwords do not match.")
}
You can't. A BOOL is either YES or NO . There is no other state.
Optionals no longer implicitly evaluate to true when they have a value and false when they do not, to avoid confusion when working with optional Bool values. Instead, make an explicit check against nil with the == or != operators to find out if an optional contains a value.
Swift recognizes a value as boolean if it sees true or false . You can implicitly declar a boolean variable let a = false or explicitly declare a boolean variable let i:Bool = true .
you need to wrapped it with !
instead of ?
.
That will solved the error message:
if (username!.isEmpty) .....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With