这是我收到错误的代码:
- 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;
- }
- }
感谢帮助.
谢谢你的时间
解决方法
Optionals不再被视为布尔表达式(如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.
所以你必须明确如下:
- if (fieldValue == nil || ...
我记得在测试版6中有所改变 – 你使用的是beta 5吗?