可选类型'$ 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
问题描述
以下是我收到错误的代码:
for (key, value) in info {
let fieldValue: AnyObject? = value
if (!fieldValue || fieldValue?.length == 0) { // this line gives the error
informationComplete = false;
}
}
这就是XCode建议我使用哪个导致另一个错误:
for (key, value) in info {
let fieldValue: AnyObject? = value
if ((!fieldValue || fieldValue?.length == 0) != nil) { //bool not convertible to string
informationComplete = false;
}
}
感谢帮助。
感谢您的时间
推荐答案
选项不再被视为布尔表达式(如Swift中所述)参考 - 修订历史记录):
Optionals are no longer considered boolean expression (as stated in the Swift Reference - Revision History):
Optionals在有值时不再隐式评估为true,如果没有,则不再为false使用可选的Bool值时避免混淆。相反,使用==或!=运算符对nil进行显式检查,以确定可选项是否包含值。
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你必须明确如下:
so you have to make it explicit as follows:
if (fieldValue == nil || ...
我记得在测试版6中有所改变 - 你使用的是beta 5吗?
I remember that changed in beta 6 - were you using beta 5?
这篇关于可选类型'$ T11'不能用作布尔值;自从安装XCode 6 beta 7后测试'!= nil'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!