How to implement Closures in Swift

Posted By : Gunjan Gumber | 23-Jun-2017

In this blog I’ll explain about the Closures used in swift

 

Closures are almost same as functions but they do not  have a name.

Closures in Swift are similar to blocks in C and Objective-C

Closures is a block of functionality that we can pass around in our code as a parameter of function or we can store it as property of object

 

Closures are enclosed in curly braces{} and are defined by a function type ( ) -> ( ), where -> separates the arguments and the return type, followed by the in keyword which separates the closure header from its body

 

The general syntax for declaring closures is:

{ (parameters) -> returnType in
statements
}

 

 

 

Simple Example for closure -

var msg: () -> (String) = {
return “Learning about Closures!”
}
msg()

 

 

 

Their are 3 kind of closures :-

 

Global functions - which have a name and cannot capture any values

Nested functions - which have a name and can capture values of constants and function they are defined in.

Closure Expressions - these are the unnamed closures and can capture values of constants and variable of context they are defined in .

 

 

 

The two cases where closures are mostly used :-

 

 1) Completion blocks  - when we have some task that will take time and we want to be notified when that task is finished, then we use closures for that 

 2) Higher Order Functions - we can use closures as input parameters for higher order functions,  

for example :

 

 let array = [1, 2, 3]
let smallerThanTwo = array.filter {$0 < 2}

 

 

 

 

With this, we can filter out the numbers that are smaller than 2

Closures are also used in the Cocoa frameworks – which are used to develop iOS or Mac applications.

 

 

Closures with known types:

 

When the type of the closure’s arguments are known, we can do as :

 func doMultiplication(value: Int, multiFunction: Int -> Int) -> Int {

return multiFunction(value)
}
doMultiplication(2, {value in

value * 3

})

 

Closures shorthand argument names:

 

Closure arguments can be references by position($0,$1, ….) rather than by name

 doMultiplication(2, {$0 * 3})
 

 

 

 

 

Furthur, We can also omit the parenthesis when a closure is the last argument of a function, parenthesis can be omitted as such:

 doMultiplication(2) {$0 * 3}
 

 

 

 

 

Trailing closure Syntax

 

Trailing closure syntax makes common code more pleasant to read and write. If the final parameter of a function is a closure, swift allows us to write it after the function call

For example:-

 func test(name: String, closure: ( ) -> ( )) {

print(“Hi, \(name)!”)
closure( )
}

 

 

Using trailing closure syntax, this will print a msg and run a closure because the closure is the final parameter of the function

 test(name: “Alice”) {
print(“the closure was executed”)
}
 

 

 

 

 

Closures are Reference types

 

This means that when we assign a closure to more than one variable they will refer to the same closure

 var sum: (Int) -> (Int) = { x in
return  x+2
}
sum(2)

var testSum = sum
testSum(3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

About Author

Author Image
Gunjan Gumber

Gunjan is a bright IOS developer with good knowledge in Swift ,Json, quite good at building user interface.

Request for Proposal

Name is required

Comment is required

Sending message..