Swift Optional keyword in condition statement
These days, I am reading tech articles on way home.
Today I read Swift interview questions and found out an weird code.
if case let v? = b {
print(v)
}
I used this optional like this.
var b: Int? = 0
if let v = b {
let c = v is Int // true
}
But I didn’t know if case or let v?
Therefore I test it on Swift playground
The unwrapped value without case is not optional.
However the value with case is optional
This is weird, I have never seen case out of switch statement.
I couldn’t find anything about if let case.
But I tracking with some tests.
When I assigned nil into Variable b, if case let v block was running.
However if case let v? block was not working.
Yes, case let v? is mean an unwrapped typeof b.
But I am curious why ? not ! 🤔
We use ! to unwrap optional type variable.
Why not?
if case let v! = b
I don’t know yet when I have to use if case let yet. 😭
I guess if let v is equal to if case let v?