Why if let delegate = delegate?

in page 428 … you wrote

if let delegate = delegate

this is the first time u did that … although we passed many delegates before and u didnt do that

thx in advance

If you look at the previous block of code to the one you refer, you will notice that the delegate is accessed using optional notation, like this:

delegate?.listDetailViewController(self, didFinishAdding: checklist)

That’s one way to access methods from your delegate. There, if the delegate is nil, the method (listDetailViewController(:didFinishAdding:)) is never called.

But in the above code, you also have to use optional chaining and use the ? each time you refer to the delegate since it’s an optional value.

In the code you mentioned:

if let delegate = delegate

You employ a technique called shadowing to get an explicit, non-optional version of the delegate variable for the duration of the if condition. So within the if, any reference to delegate is to the unwrapped version of delegate and not the optional version. And since the code inside the if is only executed if delegate is not an optional, you don’t need to use the ? for delegate within the if block either.

Does that make sense?

thx as usual … but my question why u did that technique in that delegate specifically and not the previous ones … hope that my Q is clear

It was just to show different ways to do the same thing - no particular reason for picking that specific delegate instead of the previous ones :slight_smile:

This topic was automatically closed after 166 days. New replies are no longer allowed.