Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here is the code where I'm getting the error:

for (key, value) in info {
    let fieldValue: AnyObject? = value

    if (!fieldValue || fieldValue?.length == 0) { // this line gives the error
        informationComplete = false;
    } 
}

This is what XCode suggests I use which causes another error:

for (key, value) in info {
    let fieldValue: AnyObject? = value

    if ((!fieldValue || fieldValue?.length == 0) != nil) { //bool not convertible to string
        informationComplete = false;
    }
 }

Help is appreciated.

Thanks for your time

like image 922
LondonGuy Avatar asked Sep 03 '14 15:09

LondonGuy


1 Answers

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?

like image 188
Antonio Avatar answered Nov 03 '22 01:11

Antonio