I have a question about substitution in Swift's if statement.

Asked 3 months ago, Updated 3 months ago, 9 views

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
}

ios swift

2022-09-30 11:34

1 Answers

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
}


2022-09-30 11:34

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.