Reason for assigning an optional new variable in a conditional expression in Swift

I look through quick documents, and in the optional segment I speak about the use of a question mark - ? - to indicate variables that may be zero. This can be used in the if statement to check for nil, but in the documents they assign an optional new variable in the conditional expression. Is there a reason for this?

For example, it is presented in documents 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 works 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 any reason to assign optionalString new string variable in the conditional expression?

+7
swift
source share
2 answers

Take a look at the Optional conversations section in documents. In the example you are quoting, there is not much difference. But in other cases, the if-let construct allows you to get the expanded value obtained from a series of additional references and method calls, without using implicit unpacking, which can lead to your application crash if you did not take into account all possible bindings for the value in the chain.

This is also useful if you want to avoid recalculating the value. You can use it in much the same way as you use the assignment in the (Obj) C conditional expression (remember if (self = [super init]) ).

For example, if the optional test result 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 this onto the playing field along with the implementation of checkTouchID() , which returns checkTouchID() , and you will immediately see in the results checkTouchID() that the optionalName getter performs 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 the if-let construct instead, ll will execute only once.

In a series of chain options (for example, if let johnsStreet = john.residence?.address?.street in the documents linked above), you do not want to rewrite the whole chain in the body of the if , and even more so, reprogram it.

+10
source share

I think the purpose of this assignment was to demonstrate the use of "let" in the if conditional. I do not see a significant difference between the code provided and your own.

From: Apple Inc. "Fast programming language." interactive books. https://itun.es/il/jEUH0.l

"If the optional value is nil, the condition is false and the code in curly brackets is skipped. Otherwise, the optional value is expanded and assigned to a constant after let, which makes the expanded value available inside the code block."

+2
source share

All Articles