Playing Around with Closures in Swift- 4 (Part -2)

Shakti Prakash
3 min readSep 21, 2018

--

Photo by Rich Tervet on Unsplash

Hello Everyone !! In the previous article we all discussed about few basic closures in Swift.If you haven’t checked it kindly check this out .In this Tutorial we will discuss about few more .

Swift automatically provides shorthand argument name to inline closures ,which can be used to refer to the values of the closure’s arguments by their names $0,$1,$2 and so on.

Shorthand Argument Names

As we discussed above Swift 4 facilitates the user to represent Inline closures as shorthand argument names by representing $0, $1, $2 — — $n.By using this Shorthand argument names inside our closures expression we can omit the closure’s argument list from its definition .The in keyword can be omitted because the closure expression is made up entirely of its body.Have a look at the following example .

So in the first example a closures is created which takes a single parameter and returns a string .here $0 refers to the first parameters .Similarly in the second examples the closures takes two parameters and by using $1 we are referring to the second arguments in the closure

Autoclosures

We know that closure can be used as the parameters in a functions .when we write @autoclosure the argument automatically gets wrapped in a closure .But there is one problem with this if we create a function which has @autoclosure then It’s common to call that functions that take autoclosures, but it’s not common to implement that kind of function. .Lets take a small example .

func simpleFunctionwithoutAutoclosure(_ parameters:()->Void){
print(“hey we just created a function without @autoclosure attributes “)
parameters()
}
simpleFunctionwithoutAutoclosure ({
print(“hello”)
})
func simpleFunctionwithAutoclosure(_ parameters:@autoclosure()->Void){
print(“hey we just created a function with @autoclosure attributes “)
parameters()
}
simpleFunctionwithAutoclosure(print(“hii”))

And the Out put is as follows

From the above two functions we have noticed that we don’t need to wrap the expression in curly braces in case of @autoclosure. That is what we would need to do if we were to pass in a closure instead of an autoclosure.

Escaping Closures

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.

Apple’s docs give an example of appending a closure that’s passed into a function to a mutable array of closures within your class/struct:

var completionHandlers: [() -> Void] = []func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}

As we can see, this follows the

1. Pass closure,

2. someFunctionWithEscapingClosure returns,

3. Closure executed

If we didn’t mark the parameter of this function with @escaping, we would get a compile-time error.

--

--