Reason for assigning optional to new variable in conditional statement in Swift

前端 未结 2 1081
日久生厌
日久生厌 2021-01-05 10:31

I\'m going through the swift docs, and in the optional segment, it talks about using the question mark -- ? -- to signify variables that might be nil. This can

2条回答
  •  天涯浪人
    2021-01-05 10:46

    I think the purpose of that assignment was to demonstrate the use of "let" within the if conditional clause. I don't see a meaningful difference between the provided code and your own.

    From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/il/jEUH0.l

    “If the optional value is nil, the conditional is false and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available inside the block of code.”

提交回复
热议问题