Learning Swift Language

Swift’s Closure Escaping, Unowned, and Weak Made Easy

Using Diagram to Illustrate These Swift Concept Easier

Photo by David Hofmann on Unsplash

I once know these concepts in Swift well, but after a while of not using them, I forgot. Thanks to Jake Lin for the refresher and point to this excellent StackOverflow that explains this well.

To avoid me forgetting it again, I will try to capture this using diagram, so that in the future, with a single glance of it, I will recall what I learned.

What is @escaping?

When we send a closure into a function, by default it is non-escaping. This means the closure sent into the function will be executed as per the code execution flow.

class Container {
func runClosure(myClosure: () -> Void) {
myClosure()
}
}

When myClosure is executed, it will be blocking the flow of runClosure. Therefore myClosure will be guaranteed to complete before runClosure is completed. Hence myClosure is non-escaping.

--

--