In Swift, Closures can be challenging to understand with types.
They are used with the standard libraries and they are part of the basics you need to know when writing Swift code.
What are closures?
Closures are blocks of code that can be pasted into your code. Blocks of code are often referenced as lambdas(λ) in other programming languages.
The closure expression syntax looks as follows:-
let exampleClosure = { (parameters) -> returntype in
statements
}
The parameters in closure expression syntax can be in-out parameters, but they can not have a default value. Varied parameters can be used if you name the varied parameter. Tuples can be used as parameter types as well as return types.
Closures can be defined in three ways:
1. Global functions:- These are treated as closures that have a name but it does not capture any value.
2. Nested functions:- These are also treated as closures that have names and they can capture values from the functions which are enclosed within the function.
3. Closure expressions:- These are the unnamed closures and written in a lightweight context that can capture values from their surrounding context.
If the closure does not return any value you can add the arrow (->) and the return type. This also applies to the case where the type of closure can be inferred.
Swift Closures Examples:
Below is an example of showing different ways of using closures in a swift.
1. Closure without parameters and return types
2. Closure with only return type
3. Closure with only parameter type
4. Closure with a single parameter and return type
5. Closure with multiple parameters and return types
If we see the above example we defined multiple closures with or without parameters and return types based on our requirements.
Trailing closures
The last topic I want to cover is trailing closures. The code block is used as the last argument in which we can close the route call early and end with the next closure.
Example of an uploading method:
func upload(_ fileURL: URL, completion: (_ success: Bool) -> Void) {
/// .. Uploads the file
/// .. Calls the completion:
completion(true)
}
upload(fileURL, completion: { success in
print("File upload success is \(success)!")
})
In this case, we have used the closure as a regular parameter. We can replace this code with a trailing code block.
For example:-
upload(fileURL) { success in
print("File upload success is \(success)!")
}
As you can see, we omitted the completion parameter keyword and we are now using the closure as a block in the end.
With the help of trailing closure we can use all the early named variants like the shorthand argument names:-
upload(fileURL) {
print("File upload success is \($0)!")
}
Conclusion
Closures are an important concept in swift, and we will use them often in Swift. Closures enable us to write flexible, dynamic code that is easy both to write and understand. If you want iOS App development services then iRoid solutions is right for you.
Recent Blog Posts
Get in Touch With Us
If you are looking for a solid partner for your projects, send us an email. We'd love to talk to you!