Why would I use if and let together, instead of just checking if the original variable is nil? (Swift)

前端 未结 2 547
不知归路
不知归路 2020-12-18 06:45

In “The Swift Programming Language.” book, Apple mentions using if and let together when accessing an optional variable.

The book gives the

2条回答
  •  醉话见心
    2020-12-18 07:05

    It isn't actually needed in that case. You could just use optionalName in the if. But if optionalName was a calculated property it would have to be calculated in the conditional then again in the body. Assigning it to name just makes sure it is only calculated once.

提交回复
热议问题