Although the defer keyword was already introduced in Swift 2.0, it’s still quite uncommon to use it in projects. Its usage can be hard to understand, but using it can improve your code a lot in some places.
The most common use case seen around is opening and closing a context within a scope
Master Test Distribution & App Releases Amid App Center’s ShutdownBitrise hosts a webinar on navigating the sunsetting of Microsoft’s Visual Studio App Center. Learn how to transition smoothly to alternative platforms, optimize test and store distributions, and prepare for the future of mobile app releases. The session also includes a sneak peek of Bitrise’s new tool designed to simplify distribution workflows. Watch it now for free.
How does it work?
A defer statement is used for executing code just before transferring program control outside of the scope that the statement appears in.
func updateImage() {
defer { print("Did update image") }
print("Will update image")
imageView.image = updatedImage
}
// Will update Image
// Did update image
Order of execution with multiple defer statements
If multiple statements appear in the same scope, the order they appear is the reverse of the order they are executed. The last defined statement is the first to be executed which is demonstrated by the following example by printing numbers in logical order.
func printStringNumbers() {
defer { print("1") }
defer { print("2") }
defer { print("3") }
print("4")
}
/// Prints 4, 3, 2, 1
A common use case
The most common use case seen around is opening and closing a context within a scope, for example when handling access to files. A FileHandle
requires to be closed once the access has been finished. You can benefit from the defer statement to ensure you don’t forget to do this.
func writeFile() {
let file: FileHandle? = FileHandle(forReadingAtPath: filepath)
defer { file?.closeFile() }
// Write changes to the file
}
Ensuring results
A more advanced usage of the statement is by ensuring a result value to be returned in a completion callback. This can be very handy as it’s easy to forget to trigger this callback.
func getData(completion: (_ result: Result<String>) -> Void) {
var result: Result<String>?
defer {
guard let result = result else {
fatalError("We should always end with a result")
}
completion(result)
}
// Generate the result..
}
The statement makes sure to execute the completion handler at all times and verifies the result value. Whenever the result value is nil
, the fatalError is thrown and the application fails.