Closures in Swift

1. Syntax 2. E.g of closure

A higher order function is just a type of function that accepts function as an input and returns value of type function as output.

1. closure with one argument and output: 10 2. closure with two arguments and output: 10, Hello word
output: 200 and 100

Closures are reference types.

All variables that closure references from the context where it was created will be removed after the closure completes execution.

Context defines the scope of the variable.

1. Closure are reference type. Even if you deallocate name object, closure hold the reference 2. Result :
non-escaping closures
escaping closures

To use @autoclosure with @escaping attribute as: @autoclosure @escaping () -> return type

1. autoclosure and autoclosure with escaping 2. Result
1. Closure didn’t capture the list(Example 1) 2. Closure capture the list(Example 2)

To know more about the code and flow please download the sample project from GitHub here

--

--

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