Swift Closures

In this tutorial you will learn about the Swift Closures and its application with practical example.

Swift Closures

Closures are anonymous functions consists of self-contained blocks of code and that can passed around in our code as a function parameters.In swift, we can assign closure references to constants or variables, later we can access or retrieve the value of closure based on our requirements.

Closures are like functions but they do not have name associated with it.Generally swift functions are considered as a special kind of closures and closures can take any one of three form –

Global functions :- These are closure having a name and cannot capture any values.
Nested functions :- These are closure having a name and can capture values from their enclosing functions
Closure expressions :- These are closure having a name and can capture values from their context

Declaring a closure

In Swift, there are two ways we can define a closure.

Closure expressions –

In this method a closure is defined using curly brackets { } followed by parameters of the closure wrapped in a pair of parentheses, separated from the return type by the -> symbol and in keyword separate the closure header from closure body.
Syntax:-

In case closure does not return any value we can omit the arrow (->) and the return type as following –
Syntax:-

Example:-

Output:-

 

Trailing Closure –

If the last parameter of a function is a closure, then we can put closure definition directly after the function call instead passing it as a function parameter. It is termed as trailing closures.

Recommended:-  Swift Operators

Example:-

Output:-

swift_trailing_closures

Swift Closures Type Inferring

In Swift, type inference allows to infer the return type from the context in which the closure is defined so we can remove (->) sign and return type to create short form of using closures in swift.

Example:-

Output:-

swift_closure_type_inference

Swift Closures with Implicit Returns

If closures body contains only one statement which return the result in that statement, so we choice to omit the return keyword.

Example:-

Output:-

swift_closure_implicit_return

Swift Shorthand Parameter Names

Swift provides a shorthand method to access the closures parameters, in which it provides a list of shorthand names for parameters.In order to use shorthand parameter names you need to ignore the first part of the declaration then you can refer the parameters as $0, $1, $2 and so on.

Example:-

Output:-

swift_closure_shorthand_parameters_name

Swift Closures to Capture Values

In swift closure can hold the references of any variables or constants defined in its surrounding and later it can access or retrieve the value of those variables and constants based as per the requirement. Closure is allowed to modify variable or constant for which it holds the reference till program stops execution.

Recommended:-  Swift Higher Order functions

Example:-

Output:-

swift_closure_capture_values

Swift Closures Are Reference Types

In Swift closures are of reference types, which means if a closure is assigned to more than one variable or constant they will be pointing to the same closure.

Example:-

Output:-

swift_closures_are_reference_type

Swift Escaping Closure

non escaping closure –

A closure that is passed to a function as a parameter is said to be non escaping closure it is being called before the return statement.

Recommended:-  Swift Functions

escaping closure –

A closure that is passed to a function as a parameter is said to be escaping closure it is being called after the return statement.In order to allow a closure to escape function body we need to add @escaping keyword before the parameter type to indicate that it escaping closure.

Example:-

Output:-

swift_escaping_closure

Swift Autoclosures

In swift autoclosure allows to wrap closure expression passed as an argument to a function.In order to allow a closure to wrap closure statements we need to add @autoclosure keyword before the parameter type.

Example:-

Output:-

swift_autoclosure

In this tutorial we have learn about the Swift Closures and its application with practical example. I hope you will like this tutorial.