I have a question about Swift.
I don't know why the code below looks like the result.
For iflet n=maybe, do you substitute the variable maybe for the variable n?
Please let me know if you know more.Thank you for your cooperation.
varmaybe: Int?=1
if let n = maybe {
n+1
// 2
}
For iflet n=maybe, is the variable maybe substituted for the variable n?
let
, so n
is a constant, not a variable, but it is correct to understand that you have substituted it.
This syntax is called Optional Binding
and is characterized by not executing the then clause if the substitution result is nil
.
varmaybe: Int?=nil
if let n = maybe {
n+1
// not doing
}
© 2024 OneMinuteCode. All rights reserved.