I'm getting an error when trying to check if an optional variable is set or not.
Error: Type CGPoint? does not confirm to protocol 'BooleanType.Protocol'
This is my code:
var point : CGPoint?
if (point) {
...
}
Isn't this how optional types in Swift are supposed to be used?
How should the if-comparison be written?
Since beta 5, you should write point == nil
or point != nil
.
This change was made because of confusion when the value was an optional boolean. For example:
let maybe : Bool? = false
if maybe {
// executed because `maybe` is an optional having a value (false),
// not because it is true
}
You can also use the conditional assignment as before:
if let assignedPoint = point {
/* assignedPoint is now a CGPoint unwrapped from the optional */
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.