简体   繁体   English

可选类型'$ T11'不能用作布尔值; 自从安装XCode 6 beta 7以来,测试'!= nil'

[英]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: 这就是XCode建议我使用它导致另一个错误:

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 谢谢你的时间

Optionals are no longer considered boolean expression (as stated in the Swift Reference - Revision History ): Optionals不再被视为布尔表达式(如Swift参考 - 修订历史中所述 ):

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. Optionals在有值时不再隐式评估为true,而在没有值时则不再为false,以避免在使用可选的Bool值时出现混淆。 Instead, make an explicit check against nil with the == or != operators to find out if an optional contains a value. 相反,使用==或!=运算符对nil进行显式检查,以确定可选项是否包含值。

so you have to make it explicit as follows: 所以你必须明确如下:

if (fieldValue == nil || ...

I remember that changed in beta 6 - were you using beta 5? 我记得在测试版6中有所改变 - 你使用的是beta 5吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM