How do Optional and Escaping work?
Question: Why don’t we need to add `escaping` for closure if the closure is optional?
Frankly, I think this question has little to do with programming, but it’s just like a story to me, interesting and good to know how it works exactly. Maybe you can think about it before you read this article.
If you realize you don’t know the answer too, this article may give you some answer.
The first time I saw this question, I was locked in contemplation. In hindsight, I always add the prefix word `escaping ` in front of the closure when the compile is suggested. I never noticed that we don’t need to add `escaping` if the closure is optional.
If you too are curious about why this happens, this article may have an answer for you.
Before we start, you should better know what is `optional`, `closure` and `escaping`. I mean, we need to know not just how to use but how they work exactly.
What is Optional?
Optional is a powerful and convenient thing while a developer is developing, it will mark the property which might have no value(Nil). Once the property is marked as optional, compile will force you to think about what should do if this property is nil, it could efficiently make your code much more stable.
Long story short, optional is exactly an Enum.
Optional("Gordon")
Optional("Jack")
name is nilIn the above example, we explain what optional really looks like. It's exactly an enum that has two cases(.none and .some), case `none` represents nil, and case `some` represent the value it holds (with associate value-Wrapped).
So you can see line 6 has no difference from line 8 which is the way people usually set a value to an optional type.
Even for a more clarify explanation, we can use a switch case to demonstrate in line 12 to 17, it also does the same thing to unwrap the optional.
There is another thing called `Optional chaining`.
In the above example, GrandParents holds an optional Parents, and Parents hold an optional grandChildName. How can we get the value of the grandChildName?
If without the optional chaining, we will need to do a nested switch case to unwrap two optional inside of Grandparents.
Get Gordon Feng’s stories in your inbox
Join Medium for free to get updates from this writer.
but with the optional chaining, we can do it much easily as line 23 does.
What is Closure?
A sample description of Closure is “A block of functionality that can be passed around and used in your code”
The most common way to use closure is completion handler. By using closure for completion handler, we can set a task in the closure, and execute it when the specific task has been done.
What is escaping in front of closure?
As we know, closure allows us to pass around and use it in our code, which means we can pass it as a parameter in the function, such as we do the completion handler.
Parameter normally will release when the function lifecycle to the end. The completion handler is one of the parameters of course, it will make sense if the closure has been released once the owner function lifecycle comes to the end. But the purpose of the completion handler is to execute the task after the specific task has been done(mostly, these tasks take time, and the owner function will be long gone.).
If the completion handle is released with the owner function, once the specific task has been done, there will be no completion handler to execute(will be nil).
So that’s why we will use the prefix word - escaping, to allow the closure escaping from the owner function lifecycle, allow the closure exist if the function end.
If the closure has possible to execute out of the owner function lifecycle, compile will warn you that there is an escaping closure used in the function and need to add escaping in front of the closure.
After adding the escaping, completion can escape from the reloadScreen function lifecycle.
So why we don’t need to add escaping in front of the closure?
When you read this, you must know optional is an Enum now, and escaping is only for closure to escape from the owner function. So, if we set the completion handler in reloadScreen to optional, this will happen.
Basically, escaping will only add in front of a closure, optional is an enum, so it doesn’t make sense to put escaping for an enum.
Another thing is, closure is non escaping by default in function as parameters, but something like enum associate value, struct, etc, are escaping by default.
So that will be all in today’s article, if you think this is interesting like I do, please give me some claps, thanks:)











