Difference between Non-Escaping and Escaping Closures in Swift

Experienced iOS and React Native Developer with 10+ Years of Expertise - Available for New Opportunities. Connect Now!
Search for a command to run...

Experienced iOS and React Native Developer with 10+ Years of Expertise - Available for New Opportunities. Connect Now!
Get ready for your iOS interview with our comprehensive blog post series covering common and challenging questions. Learn tips and examples in each post!
In this blog post, we will explore the difference between reference equality and value equality in Swift, and how to use them correctly. What is Reference Equality? Reference equality is a way of comparing two objects based on their memory address. I...
This time, I'm exploring the fascinating world of AI, stepping away from my usual focus on iOS development. There's so much more to share about AI, and I can't wait to dive into it with you! If you'd like to follow along and receive regular updates, ...

The app is Time Tango, a Pomodoro timer app. I chose this concept after conducting keyword research for popular terms that face minimal competition on the app store. The app is free to use and received approval in its first review. The strategy focus...

In my last article, I talked about how data flows in SwiftUI. Now, Apple has released something new in iOS 17 - the Observation Framework. This changes how SwiftUI works with data. In this article, we will look at how to use the new Observable Macro ...

One of the critical elements of SwiftUI is its approach to managing data flow within applications. This comprehensive guide will provide you with a strong understanding of data flow in SwiftUI, how it operates, and why it's vital for your SwiftUI app...

Arrays are one of the most common data structures used in programming. In Swift, arrays are used to store ordered lists of values of the same type. They are incredibly versatile and powerful, but using them efficiently is key to writing high-performi...

In Swift, closures can be categorized as either escaping or non-escaping depending on their behaviour. In this blog post, we'll explore the difference between these two types of closures and how they can be used in your code.
A closure is a self-contained block of functionality that can be passed around and used in your code. It can capture and store references to any constants and variables from the context in which it was defined, allowing it to access those values later. Closures in Swift are similar to blocks in Objective-C and lambdas in other programming languages.
Here is a simple example of a closure in Swift:
let numbers = [1, 2, 3, 4, 5]
let sortedNumbers = numbers.sorted { $0 < $1 }
print(sortedNumbers) // [1, 2, 3, 4, 5]
In this example, we're using the sorted method on an array of numbers. The sorted method takes a closure as its argument that defines how the numbers should be sorted. In this case, we're using a closure that compares two numbers and returns true if the first number is less than the second number.
An escaping closure is a closure that is passed as a parameter to a function but is called after the function returns. This means that the closure is stored somewhere and can be called later, even after the function that originally received the closure has finished executing. Escaping closures are marked with the @escaping attribute.
Here is an example of a function that takes an escaping closure as a parameter:
func fetchData(completion: @escaping (Result<[String], Error>) -> Void) {
// Perform some asynchronous operation to fetch data
// When the operation completes, call the completion closure with the result
let result: Result<[String], Error> = .success(["John", "Jane", "Jack"])
completion(result)
}
In this example, the fetchData function takes a closure as a parameter that is used to handle the result of an asynchronous operation. The closure is marked with the @escaping attribute because it will be called after the fetchData function returns.
A non-escaping closure is a closure that is passed as a parameter to a function and is called before the function returns. This means that the closure cannot be stored and called later outside of the function. Non-escaping closures do not need to be marked with any attributes.
Here is an example of a function that takes a non-escaping closure as a parameter:
func processNumbers(numbers: [Int], closure: (Int) -> Void) {
for number in numbers {
closure(number)
}
}
In this example, the processNumbers function takes an array of numbers and a closure as parameters. The closure is used to process each number in the array and is called immediately inside the function.
In summary, closures are self-contained blocks of functionality that can be passed around and used in your code. They can be categorized as either escaping or non-escaping depending on their behavior. Escaping closures are passed as a parameter to a function but are called after the function returns, while non-escaping closures are called before the function returns. Understanding the difference between these two types of closures is important for writing efficient and safe code in Swift.
Let me know if you have any queries.