I'm reading Swift Programming Language book to get familiar Swift language as many others.
It says that
In an if statement, the conditional must be a Boolean expression—this means that code such as if score { ... } is an error, not an implicit comparison to zero.
What i understand that condition must evaluate a bool value. Even if it's a integer value, it will not work. But what i don't understand is
if let convertedRank = Rank.fromRaw(3) {
let threeDescription = convertedRank.simpleDescription()
}
How does this evaluate a bool value. Normally, we use let to create a constant. As implicit conversion isn't possible in Swift, how does let convertedRank = Rank.fromRaw(3)
evaluate a bool value which is a must for condition ?
In addition to Bools, if
statements can also take optionals, with the condition evaluating to whether or not the optional has a value. This particular construct is called "optional binding".
fromRaw()
returns an optional of the type. Because there isn't a Rank for every Int, the function will return either a Rank or nil as a Rank optional. Optionals are the exception to only having Bools in an if condition. If the Optional has a value, it will evaluate as true and if the Optional is nil it will evaluate as false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With