Defer in Swift explained with Code Examples Swift 6.4 introduces SE-0493, allowing async calls in defer bodies for asynchronous cleanup, as explained in a SwiftLee article. The defer keyword executes code just before leaving scope, with multiple defer statements running in reverse order. A common use case is closing file handles, and the new feature lets developers use await inside defer blocks in async contexts, keeping setup and teardown together. 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. Starting in Swift 6.4, defer can also await asynchronous cleanup when used inside an async context, making it even more useful for modern Swift Concurrency code. Share your AI agent’s work with your team https://display.dev/?utm source=swiftlee&utm medium=newsletter&utm campaign=swiftlee sponsor Your agent generates a report, dashboard, or prototype as HTML or Markdown. display.dev turns it into a secure URL your team can open with their existing work login, comment on inline, and send back to any agent through MCP. Works with Cursor, Codex, Claude Code, and more. No viewer accounts, no seat management, no vendor lock-in. Learn more https://display.dev/?utm source=swiftlee&utm medium=newsletter&utm campaign=swiftlee sponsor . 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 } // Prints: // 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 The same reverse order applies when a defer body contains asynchronous work in Swift 6.4: each deferred block runs on scope exit, and asynchronous work is awaited before continuing more about this later . 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 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. js func writeFile { let file: FileHandle? = FileHandle forReadingAtPath: filepath defer { file?.closeFile } // Write changes to the file } Awaiting asynchronous cleanup with defer Swift 6.4 implements SE-0493: Support async calls in defer bodies https://github.com/swiftlang/swift-evolution/blob/main/proposals/0493-defer-async.md , allowing you to use await inside a defer block when the surrounding context is already asynchronous. Before Swift 6.4, cleanup that required await had to be repeated across multiple exit paths or moved into a separate unstructured task. You can now keep setup and teardown close together: js func importArticles async throws { let importer = ArticleImporter await importer.open defer { await importer.close // Allowed from Swift 6.4 } try await importer.importLatestArticles } The defer body still runs when leaving scope, just like synchronous defer statements. The difference is that Swift now implicitly awaits the asynchronous cleanup before the function returns. If you are new to this syntax, start with my article on async/await in Swift or read more about structured concurrency. Why not create a Task? You might wonder why you can’t simply use a Task like you did before Swift 6.4: js func importArticles async throws { let importer = ArticleImporter await importer.open defer { Task { await importer.close } } try await importer.importLatestArticles } While this compiles, it starts unstructured work and allows importArticles to return before cleanup finishes. In other words, you lose the guarantee that made defer useful in the first place. 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. php func getData completion: result: Result