Escaping Closures in Swift

Escaping Closures

A closure is said to be escaping when a closure passed in as an argument and executed after the scope of a function or when a function is returned. You can think of it as the closure “escaping” the body of a function.

Conclusion

To conclude there are several benefits to marking closures as @nonescape by default. The most obvious benefits are performance and the ability for the compiler to optimise your code. This also means that you can get away with with using weak references to self in a @nonescape closure because it is invoked before the function returns.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Melvin John

Melvin John

A Software Engineer with a passion for technology. Working as an iOS Developer @BBC