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

swift

Solution

Take a look at the section on Optional Chaining in the docs. In the example you cite, there's not much difference. But in other cases, an `if-let` construction lets you get at an unwrapped value that comes from a series of optional references and method calls, without using implicit unwraps that can crash your app if you haven't considered all the possible bindings for a value in a chain.

It's also useful if you want to avoid recomputing a value. You can use it in a lot of the same ways you'd use an assignment in a conditional in (Obj)C (remember `if (self = [super init])`).

For example, if the optional being tested comes from a computed property:

var optionalName: String? {
get {
    if checkTouchID() {
        return "John Appleseed"
    } else {
        return nil
    }
}
}
var greeting = "Hello!"
if optionalName != nil {
    greeting = "Hello, \(optionalName)"
}

Paste that into a playground, along with a stub implementation of `checkTouchID()` that returns `true`, and you'll immediately see in the results area that the `optionalName` getter is executing twice. (This would be a problem in a more realistic scenario, because you probably don't want code like this to implicitly `checkTouchID()` or `downloadFromServer()` or `billApplePay()` twice.) If you use an `if-let` construction instead, you'll only execute the getter once.

In a series of chained optionals (like `if let johnsStreet = john.residence?.address?.street` in the docs linked above), you don't want to rewrite the whole chain in the body of the `if` statement, much less recompute it.

Problem

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 be used in an if statement to check for nil, but in the docs they assign the optional to a new variable in the conditional. Is there a reason for this? For Example, it is presented in the docs similar to this: ``` // Declare an optional string (might be nil) var optionalString: String? = "Hello" // Assigns optionalString to new variable before checking if nil if let string = optionalString { println("\(optionalString) is not nil!") } else { println("\(optionalString) is nil") } ``` However, this runs just fine for me in tests: ``` var optionalString: String? = "Hello" // Assigns optionalString to new variable before checking if nil if optionalString { println("\(optionalString) is not nil!") } else { println("\(optionalString) is nil") } ``` Question Is there a reason to assign `optionalString` to a new variable `string` in the conditional statement?

Original source