Swift Closures
Swift Closures
Closures in Swift are similar to that of self-contained functions organized as blocks and called
anywhere like C and Objective C languages. Constants and variable references defined inside the
functions are captured and stored in closures. Functions are considered as special cases of
closures and it takes 3 forms.
Have a name. Do not Have a name. Capture values Unnamed Closures capture values
capture any values from enclosing function from the adjacent blocks
Closure expressions in Swift language follows crisp, optimization and light weight syntax styles
which includes.
Syntax
Following is a generic syntax to define closure which accepts parameters and returns a data type:
Expressions in Closures
Convenient way of naming and defining blocks of code are achieved through nested functions.
Instead of representing the whole function declaration and name constructs are used to denote
shorter functions. Representing the function in a clear brief statement with focused syntax is
achieved through closure expressions.
Array contents Int, Int and returns a Boolean value Bool if the array is sorted properly it will
return true value otherwise it will return false.
Normal function with input string is written and passed to the sorted function to get the strings
sorted to new array which is shown below
true
The initial array to be sorted for icecream is given as "Swift" and "great". Function to sort the array
is declared as string datatype and its return type is mentioned as Boolean. Both the strings are
compared and sorted in ascending order and stored in a new array. If the sorting is performed
successful the function will return a true value else it will return false.
constant parameters.
inout parameters.
Closure expression did not support default values. Variadic parameters and Tuples can also be
used as parameter types and return types.
30
The parameters and return type declarations metioned in the function statement can also be
represented by the inline closure expression function with 'in' keyword. Once declaring parameter
and return types 'in' keyword is used to denote that the body of the closure.
To return a Single expression statement in expression closures 'return' keyword is omitted in its
declaration part.
println(descending)
println(ascending)
The statement itself clearly defines that when string1 is greater than string 2 return true otherwise
false hence return statement is omitted here.
-10
Here, 0and1 refer to the closure's first and second String arguments.
200
Swift facilitates the user to represent Inline closures as shorthand argument names by
representing 0, 1, 2 − − − n.
Closures argument list is omitted in definition section when we represent shorthand argument
names inside closure expressions. Based on the function type the shorthand argument names will
be derived. Since the shorthand argument is defined in expression body the 'in' keyword is
omitted.
Closures as Operator Functions
Swift provides an easy way to access the members by just providing operator functions as
closures. In the previous examples keyword 'Bool' is used to return either 'true' when the strings
are equal otherwise it returns 'false'.
Closures as Trailers
Passing the function's final argument to a closure expression is declared with the help of 'Trailing
Closures'. It is written outside the function with {}. Its usage is needed when it is not possible to
write the function inline on a single line.
import Foundation
var letters = ["North", "East", "West", "South"]
Capturing constant and variable values is achieved by using nested function by writing function
with in the body of other function.
In Swift when the constant or variable is declared inside a function, reference to that variables are
also automatically created by the closure. It also provides the facility to refer more than two
variables as the same closure as follows
Here oneDecrement and Decrement variables will both point the same memory block as closure
reference.
82
64
46
When each and every time the outer function calcDecrement is called it invokes the decrementer
function and decrements the value by 18 and returns the result with the help of outer function
calcDecrement. Here calcDecrement acts as a closure.
Eventhough the function decrementer does not have any arguments closure by default refers to
variables 'overallDecrement' and 'total' by capturing its existing values. The copy of the values for
the specified variables are stored with the new decrementer function. Swift handles memory
management functions by allocating and deallocating memory spaces when the variables are not
in use.
Loading [MathJax]/jax/output/HTML-CSS/jax.js