我在尝试检查是否设置了可选变量时遇到错误.
Error: Type CGPoint? does not confirm to protocol 'BooleanType.Protocol'
这是我的代码:
var point : CGPoint?
if (point) {
...
}
Run Code Online (Sandbox Code Playgroud)
这不是应该如何使用Swift中的可选类型吗?
应如何编写if-comparison?
Jes*_*per 10
从beta 5开始,你应该写point == nil或point != nil.
当值是可选的布尔值时,由于混淆而进行了此更改.例如:
let maybe : Bool? = false
if maybe {
// executed because `maybe` is an optional having a value (false),
// not because it is true
}
Run Code Online (Sandbox Code Playgroud)
您也可以像以前一样使用条件赋值:
if let assignedPoint = point {
/* assignedPoint is now a CGPoint unwrapped from the optional */
}
Run Code Online (Sandbox Code Playgroud)