Optional Type 'Bool' Cannot Be Used as a Boolean; Test for '!=Nil' Instead

optional type 'Bool' cannot be used as a boolean; Test for '!=nil' instead

You are checking if the value is different of nil and returning if it is, based un your comments and your second if you probably want to check if it is nil.

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)
}))
}

Optional type '(() - Bool)?' cannot be used as a boolean; test for '!= nil' instead in delegate function call

You need to call the function, not reference the function.

Change:

if self.delegate?.selectedInterfaceOrientationIsLandscape  {

to:

if self.delegate?.selectedInterfaceOrientationIsLandscape()  {

But since you are using optional chaining, it should be:

if self.delegate?.selectedInterfaceOrientationIsLandscape() ?? false {

optional type () cannot be used as boolean; test for '!=nil' instead

the method you use is:

func ABAddressBookCreateWithOptions(_ options: CFDictionary!, _ error: UnsafeMutablePointer<Unmanaged<CFError>?>) -> Unmanaged<ABAddressBook>!

The return value is an optional Unmanaged object. So you cannot add a ! before it. Adding ! before a value can only be used for Bool value.

If you want to force it to have value, add ! after the method instead.

Checking the value of an Optional Bool

With optional booleans it's needed to make the check explicit:

if boolean == true {
...
}

Otherwise you can unwrap the optional:

if boolean! {
...
}

But that generates a runtime exception if boolean is nil - to prevent that:

if boolean != nil && boolean! {
...
}

Before beta 5 it was possible, but it has been changed as reported in the release notes:

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.

Addendum: as suggested by @MartinR, a more compact variation to the 3rd option is using the coalescing operator:

if boolean ?? false {
// this code runs only if boolean == true
}

which means: if boolean is not nil, the expression evaluates to the boolean value (i.e. using the unwrapped boolean value), otherwise the expression evaluates to false

Receiving Error Optional type '$T3' cannot be used as a boolean; test for '!=nil' instead

if (!PFUser.currentUser()) { 

I assume this is the problem line. It should be

if PFUser.currentUser() == nil {

Since you're testing for the value not being present. Note you don't require brackets round the if condition in swift.

Optional type '$T11' cannot be used as a boolean; test for '!= nil' instead since installing XCode 6 beta 7

Optionals are no longer considered boolean expression (as stated in the Swift Reference - Revision History):

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.

so you have to make it explicit as follows:

if (fieldValue == nil || ...

I remember that changed in beta 6 - were you using beta 5?

swift - Optional to Bool Conversion Causes Error

From Xcdoe6-beta5 release notes

Optionals no longer conform to the BooleanType (formerly LogicValue)
protocol, so they may no longer be used in place of boolean
expressions
(they must be explicitly compared with v != nil). This
resolves confusion around Bool? and related types, makes code more
explicit about what test is expected, and is more consistent with the
rest of the language.

Note that ImplicitlyUnwrappedOptional still
includes some BooleanType functionality. This issue will be resolved
in a future beta. (17110911)

So use myString != nil as it suggested. And always refer to latest documentation because Swift still in beta stage.



Related Topics



Leave a reply



Submit