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
The best mobile app monitoring product just keeps getting betterSentry automatically captures crashes recorded on macOS, iOS, and tvOS to make sure you're shipping the best experience to your users. Oh... and also Emerge Tools is now a part of Sentry, so things are getting even better. Use coupon code SWIFTLEE2025 for Sentry’s Business Plan and get started now with 2 free months.
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

FREE 5-Day Email Course: The Swift Concurrency Playbook
A FREE 5-day email course revealing the 5 biggest mistakes iOS developers make with with async/await that lead to App Store rejections And migration projects taking months instead of days (even if you've been writing Swift for years)
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.