Swift Closure and its type
In Swift, closures are self-contained blocks of code that can capture and store references to variables and constants from the surrounding context in which they are defined. Closures can be used in various ways in Swift, including as function parameters, return values, and variables. Here are some examples of closures in Swift
- Basic Closure Syntax
A simple closure that takes no parameters and returns an integer:
let closureExample = {
return 42
}
let result = closureExample()
print(result) // Prints 42
2. Closure with Parameters:
A closure that takes two integer parameters and returns their sum:
let addClosure: (Int, Int) -> Int = { (a, b) in
return a + b
}
let sum = addClosure(3, 5)
print(sum) // Prints 8
3. Closure as Function Parameter
You can pass closures as parameters to functions. Here’s an example using the map
function to apply a closure to each element of an array:
let numbers = [1, 2, 3, 4, 5]
let squared = numbers.map { (number) in
return number * number
}
print(squared) // Prints [1, 4, 9, 16, 25]
4. Trailing Closure Syntax:
When a closure is the last argument to a function, you can use trailing closure syntax for cleaner code:
let result = numbers.reduce(0) { (accumulator, value) in
return accumulator + value
}
print(result) // Prints 15
5. Capturing Values:
Closures can capture and store references to variables and constants from the surrounding context. Here’s an example:
func makeIncrementer(incrementAmount: Int) -> () -> Int {
var total = 0
let incrementer: () -> Int = {
total += incrementAmount
return total
}
return incrementer
}
let incrementByTwo = makeIncrementer(incrementAmount: 2)
print(incrementByTwo()) // Prints 2
print(incrementByTwo()) // Prints 4
6. Escaping Closures:
Closures that are passed as arguments to a function but are executed after the function returns are called “escaping closures.” You need to mark them with the @escaping
keyword:
var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completion: @escaping () -> Void) {
completionHandlers.append(completion)
}
someFunctionWithEscapingClosure {
print("Closure is escaping")
}
completionHandlers.first?() // Prints "Closure is escaping"
7. Autoclosures
An autoclosure is a type of closure that automatically wraps an expression you provide as an argument into a closure. Autoclosures are often used for lazy evaluation:
var customersInLine: [String] = ["Alice", "Bob", "Charlie"]
func serveCustomer(_ customerProvider: @autoclosure () -> String) {
print("Now serving \(customerProvider())")
}
serveCustomer(customersInLine.removeFirst()) // Prints "Now serving Alice"
These examples cover various aspects of closures in Swift, from simple closures to more advanced usage, such as escaping closures and autoclosures. Closures are a powerful feature in Swift and are commonly used in asynchronous programming, functional programming, and many other scenarios.