What is @escaping keyword in Swift and the differences between @nonescaping

BEDİRHAN TURHAN
Appcent
Published in
4 min readJan 1, 2024

Hello everyone, in this article we will examine the @escaping keyword, which situations we can use, and we will also talk about the differences between @nonescaping.

If you have used functions in Swift, you have surely seen closures and escaping structures. We usually use closers to run the code we want to run after the function is finished. Our function runs then the completion handler (clouser) runs. Swift uses @nonescaping in clousers by default. If a clouser is used with the @escaping keyword, it means that the clouser will take longer or the completion block will be passed to another function. It is often used a lot in HTTP Request related operations. Swift wants us to use @escaping in such functions.
Let’s examine a function with @nonescaping clouser:

func doSomeThing(default closure: () -> Void) {
//Do something
closure()
}

The closure you write in the doSomeThing() function is executed and completed in the doSomeThing() function. The closure runs in the related function when the time comes. We specify that nothing we do in the closure will trigger in another function or cause a delay that requires it to be asynchronous with @nonescaping.

func doSomething(using closure: () -> Void) {
DispatchQueue.main.async {
closure()
}
}

If we put our clouser inside an asynchronous code blog like above, the code will cause a compiler error because Swift realizes that the clouser will be out of scope of the function it is in. It will be called in another thread, not inside the function. You need to specify this clouser with @escaping. The @escaping tag lets people who are going to use the function know that it is an async operation that may take a long time or be triggered elsewhere, it also tells the Swift compiler that we know that the clouser is leaving the function it is in and that this is okay.

Now that we understand why the @escaping tag is used, let’s look at the areas where they are used. This way it becomes more understandable. We use @escaping closers mostly in functions where we use Http methods. Because response times are long in internet transactions and therefore the closure cannot work immediately and we need to use this clouser with the @escaping tag.

func sendRequest(_ url : URL, _ completion: @escaping (Result<Data, Error>) -> Void) {
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if let error = error {
completion(.failure(error))
}

if let data = data {
completion(.success(data))
}

}
session.resume()
}

In the above, we wrote a small function to pull data from the url with URLSession. We used our closure with @escaping because when the dataTask function pulls data from the url, there is a delay, we cannot get the data synchronously, we need to wait for the response. In order not to freeze the app, we will use the @escaping keyword here in our completion block, so that this code continues to run in another thread and enters the completion block when it is finished. DataTask’s completion block is also used with the @escaping keyword.

we examine other usage of @escaping keyword. We can execute completion block to different function. This is usually use in delegate functions. we write the completion block with the first function we call, but we need the output of other functions to enter the completion.

xcode warning when not using @escaping

Let’s examine a function where we use @nonescaping. When we want to show a new ViewController, we use the present() function and this function has a completion parameter.

let vc = UIViewController()
present(vc , animated : true){
//do something after showing new vc
}

This is an @nonescaping completion. After the new ViewController is shown, you can do what you want to do in this completion. We don’t need to use @escaping here as it is not asynchronous or out of the scope operations like in our previous example.

I hope it was explanatory. Thank you for reading. I would be very grateful if you give feedback about the parts you think are missing or incorrect.

--

--