Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

stackdaemon

As per Apple docs, optional chaining is the following:

You specify optional chaining by placing a question mark (?) after the optional value on which you wish to call a property, method or subscript if the optional is non-nil. ... optional chaining fails gracefully when the optional is nil ...

My interpretation of this is that a construction as the following is optional chaining:

someMasterObject.possiblyNilHandler?.handleTheSituation()

...and that the above line would call the handleTheSituation method if the handler is not nil, and fails gracefully (line skipped) if the handler is nil.

However almost all examples I see of optional chaining use the "if let" construction, as per:

if let handler = someMasterObject.possiblyNilHandler{
  handler.handleTheSituation()
}

In fact, the documentation and examples I have found on the net make such heavy use of the "if let" construction in relation to optional chaining that it seems as if that IS optional chaining.

Am I correct, however, in assuming that my first example is a supported use of optional chaining and that the if let construction is another construction using (or being intimately tied to) optional chaining?

user2864740

The conclusion is correct - let is an independent, but useful, construct. In context it introduces a binding only within the if-body and executes the if-body only if the bound value is not-nil. (Technically it unwraps an optional binding.)

let does not affect how the expression on the right (with or without chaining) is handled. For instance, if someMasterObject were optional/nil it would fail and not "chain" - even with let.

When one or the other (or both) is more "correct" depends on the situation: eg. what is being chained and what the corrective action should be.


For instance, if someMasterObject could be nil, we might have the following which uses both chaining and let. Also note how the return value matters and is not simply discarded or "nil on failure":

if let handler = someMasterObject?.possiblyNilHandler{
  return handler.handleTheSituation()
} else {
  return FAILED_TO_CALL
}

Then compare it with a non-equivalent chained form, which would only return nil in the failed-to-call case, but nil might be a valid return value from handleTheSituation!

return someMasterObject?.possiblyNilHandler?.handleTheSituation()

On the other hand, do consider that there is always direct translation of chaining to nested if-let statements:

result_of_expression = someMasterObject?.possiblyNilHandle?.handleTheSituation()

if let master = someMasterObject {
   if let handler = master.possiblyNilHandler {
       result_of_expression = handler.handleTheSituation()
   } else {
       result_of_expression = nil
   }
} else {
   result_of_expression = nil
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Optional chaining question mark after function name

From Dev

Swift 3.0 Optional Chaining

From Dev

swift optional chaining with cast

From Dev

Optional chaining with Swift strings

From Dev

Optional chaining and Array in swift

From Dev

Are optional protocols from Objective-C not something that should be done in Swift?

From Dev

Assign value with optional question mark

From Dev

Swift - use of optional with let

From Dev

Optional Chaining or ternary expression in Swift?

From Dev

Optional let and optional var inside a class in Swift

From Dev

Swift optional chaining doesn't work in closure

From Dev

Is there something like the swift optional chaining in javascript?

From Dev

Optional chaining used in left side of assignment in Swift

From Dev

Accessing boolValue in a NSNumber var with optional chaining (in Swift)

From Dev

It it okay to access an optional variable with question mark without exclamation mark?

From Dev

Why there is a ? mark in if-let statement to see an optional has a value

From Dev

Swift 3: 'if let' optional binding error

From Dev

" if let " must be used only for optional ? (Swift)

From Java

Optional chaining (?.) in nashorn

From Dev

Optional Chaining returning an Int

From Dev

Optional Chaining in JavaScript

From Dev

Optional Chaining Not Working As Expected

From Dev

Chaining Optional.orElseThrow

From Dev

Optional chaining for constructor calls?

From Dev

Chaining multiple dialogs using context.call/done

From Dev

Swift Optional of Optional

From Dev

How to use optional chaining while searching through a dictionary in swift?

From Dev

Is Swift optional chaining lazily evaluated left-to-right?

From Dev

optional chaining in Swift 3: why does one example work and not the other?

Related Related

  1. 1

    Optional chaining question mark after function name

  2. 2

    Swift 3.0 Optional Chaining

  3. 3

    swift optional chaining with cast

  4. 4

    Optional chaining with Swift strings

  5. 5

    Optional chaining and Array in swift

  6. 6

    Are optional protocols from Objective-C not something that should be done in Swift?

  7. 7

    Assign value with optional question mark

  8. 8

    Swift - use of optional with let

  9. 9

    Optional Chaining or ternary expression in Swift?

  10. 10

    Optional let and optional var inside a class in Swift

  11. 11

    Swift optional chaining doesn't work in closure

  12. 12

    Is there something like the swift optional chaining in javascript?

  13. 13

    Optional chaining used in left side of assignment in Swift

  14. 14

    Accessing boolValue in a NSNumber var with optional chaining (in Swift)

  15. 15

    It it okay to access an optional variable with question mark without exclamation mark?

  16. 16

    Why there is a ? mark in if-let statement to see an optional has a value

  17. 17

    Swift 3: 'if let' optional binding error

  18. 18

    " if let " must be used only for optional ? (Swift)

  19. 19

    Optional chaining (?.) in nashorn

  20. 20

    Optional Chaining returning an Int

  21. 21

    Optional Chaining in JavaScript

  22. 22

    Optional Chaining Not Working As Expected

  23. 23

    Chaining Optional.orElseThrow

  24. 24

    Optional chaining for constructor calls?

  25. 25

    Chaining multiple dialogs using context.call/done

  26. 26

    Swift Optional of Optional

  27. 27

    How to use optional chaining while searching through a dictionary in swift?

  28. 28

    Is Swift optional chaining lazily evaluated left-to-right?

  29. 29

    optional chaining in Swift 3: why does one example work and not the other?

HotTag

Archive