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

1

6

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

Baeza answered 3/9, 2014 at 15:18 Comment(0)
N
11

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?

Nickienicklaus answered 3/9, 2014 at 15:20 Comment(1)
Thanks. I bookmarked that revision list. I was using XCode beta 3 or 4 previously I think.Baeza

© 2022 - 2024 — McMap. All rights reserved.