Optional Chaining vs Optional Binding in Swift

Optional binding:

Optional binding is used to check whether an optional variable or constant has a non-nil value, and if so, assign that value to a temporary, non-optional variable. It stores the value that you’re binding in a variable. For optional binding, we use the if let or if var keywords together. If we use if let, the temporary value is a constant and cannot be changed. If we use the if var keyword, it puts the temporary value into a variable that can be changed. It has the following syntax:

var myOptional: String?
if let temp = myOptional {
print(temp)
print("Can not use temp outside of the if bracket")
} else {
print("myOptional was nil")
}

Optional chaining:

Optional chaining allows us to call properties, methods, and subscripts on an option that might be nil. If any of the chained values return nil, the return value will be nil. It does not store the value on the left into a variable. The following code gives an example of optional chaining using a fictitious car object. It has the following syntax:

In this example, if either the car or tires optional variables are nil, the variable tireSize will be nil, otherwise, the tireSize variable will be equal to the tireSize property:

var tireSize = car?.tires?.tireSize

Difference between Optional Chaining and Optional Binding:

Optional Chaining
Optional Binding
1. optional chaining does not store the value on the left into a variable.
1. Optional binding stores the value that you're binding in a variable.
2. Optional chaining doesn't allows an entire block of logic to happen the same way every time.2. Optional binding allows an entire block of logic to happen the same way every time.
3. With optional chaining, you are creating a new reference, it's just wrapped.3. With optional binding, you are creating a new reference, it's just unwrapped.