You can expand it as follows:
if let yourStr = str_VAR?.toInt() { println("str_VAR = \(yourStr)") //"str_VAR = 100" println(yourStr) //"100" }
See IT for more details.
When to use if let?
if let is a special structure in Swift that allows you to check if Option has a value, and if it does, do something with the expanded value. Let's get a look:
if let yourStr = str_VAR?.toInt() { println("str_VAR = \(yourStr)") println(yourStr) }else { //show an alert for something else }
The if if structure flips str_VAR?.toInt() (i.e., checks to see if the value is stored and accepts that value) and stores its value in the constant yourStr . You can use yourStr inside the first if branch. Please note what is inside if you do not need to use? or! more. It is important to understand that yourStr has an Int type, which is not an optional type, so you can directly use its value.
Richie rich
source share