2

I am new to swift, so if this is question sounds stupid, please forgive me. Below is the example of optional binding. How it works, I understood. But why it is needed, I am unable to understand.

   var optionalName: String? = "John Appleseed"
   var greeting = "Hello!"
   if let name = optionalName {
        greeting = "Hello, \(name)"
   }

Instead of using the new construct of "If let" we can use the below code. What is the harm in it? why we are taking an extra temporary variable altogether? Thanks in advance.

var optionalName: String = "John Appleseed"
var greeting = "Hello!"
    if optionalName != nil {
        greeting = "Hello, \(optionalName)"
    }
user804417
  • 155
  • 2
  • 13
  • 3
    Try both sets of code. Notice the difference in output? – rmaddy Dec 17 '16 at 05:53
  • 1
    Related: [When should I compare an optional value to nil?](http://stackoverflow.com/questions/29717210/when-should-i-compare-an-optional-value-to-nil). – Martin R Dec 17 '16 at 05:58
  • Thanks @rmaddy for pointing out the mistake! Now both will give the same output. Now which one should I go for & in which case? – user804417 Dec 17 '16 at 06:10
  • @MartinR Thanks for pointing out to the awesome question link with so much elaborated answer! – user804417 Dec 17 '16 at 07:52

1 Answers1

2

The if let construct allows you to "unwrap" the optional so that the value can be used (when it is not nil).

Simply testing for not nil does not change the fact that you have an optional.

So the if let construct allows you to test and unwrap the optional in one step. It is a convenience and provides a safe way for you to work with an optional.

There is no harm in just testing for not nil, as long as you do not expect to use the wrapped value in any meaningful way (i.e. you are just testing for not nil). Or you plan on force unwrapping with the ! operator every time you want to use the value within the same scope.

In most cases you will probably want to use the value if its not nil and again the if let construct provides a safe and convenient way of doing so.

Ryan H.
  • 2,543
  • 1
  • 15
  • 24
  • Thanks Ryan for your answer. Your answer clarified my thought quite a extent. My further question is: so you mean optionalName! & the alternative if-let construct caters the same purpose? Can you point me out any specific case where I should definitely go for if-let and not using forceful unwrapping in every step? – user804417 Dec 17 '16 at 06:43
  • 1
    @user804417 Just to be clear, the **combination** of testing for not nil and force unwrapping is equivalent to the `if let` construct. Simply force unwrapping on its own means that you know for **certain** that the value will not be nil at runtime. – Ryan H. Dec 17 '16 at 07:12
  • @user804417 I cannot think of a definitive example at the moment, however my *advice* would be to use `if let` first and fallback to testing for not nil if that does not meet your needs. I think you would get similar advice from other Swift developers, but overall any answer would probably be to some degree subjective. – Ryan H. Dec 17 '16 at 07:16
  • It will be highly appreciable if any body can help to find a specific scenario where optional binding is absolutely necessary! What I believe Apple introduced this construct to minimize run-time crashes & show error at compile time at first place! Other languages are still surviving with out this construct, there must be scenario why Apple has introduced it in Swift! – user804417 Jan 31 '17 at 10:07