Multiple defer Blocks Ordering
Understand reverse execution order of defers.
Multiple defer Blocks Ordering is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Multiple defers
A scope can contain several defer blocks. They all run at scope exit.
func demo() {
defer { print("A") }
defer { print("B") }
print("body")
}
demo()Reverse (LIFO) Order
Deferred blocks run in reverse order of declaration: last declared runs first.
func order() {
defer { print("1") }
defer { print("2") }
defer { print("3") }
print("start")
}
order()Why LIFO
LIFO ordering mirrors how setup and teardown nest: the most recent setup is undone first.
func resources() {
print("open A")
defer { print("close A") }
print("open B")
defer { print("close B") }
print("use both")
}
resources()Stacking Mental Model
Think of defers as a stack: each one pushes a task, and they pop off in reverse when the scope ends.
func stack() {
defer { print("pop first-declared last") }
defer { print("pop last-declared first") }
print("push done")
}
stack()Defers and Local State
Each defer captures the surrounding variables and sees their final values when it runs.
func state() {
var x = 1
defer { print("x is", x) }
x = 42
}
state()Defers in a Loop
Inside a loop, each iteration registers and runs its own defers in LIFO order at the end of that iteration.
for i in 1...2 {
defer { print("late", i) }
defer { print("early", i) }
print("body", i)
}Nested Scopes
Defers run when their own scope exits, so an inner block's defer runs before an outer block's.
func nested() {
defer { print("outer defer") }
do {
defer { print("inner defer") }
print("inner body")
}
print("outer body")
}
nested()Predictable Cleanup
LIFO order means matched open/close pairs naturally close in the right sequence.
func files() {
print("open log")
defer { print("flush log") }
print("open db")
defer { print("disconnect db") }
print("transaction")
}
files()Reading the Order
To predict output, read defers bottom-up: the last defer in the scope prints first.
func quiz() {
defer { print("one") }
defer { print("two") }
print("body runs first")
}
quiz()Combining With Return
All registered defers still run in LIFO order even when the function returns early.
func early(_ skip: Bool) {
defer { print("cleanup 1") }
defer { print("cleanup 2") }
if skip { return }
print("full body")
}
early(true)Best Practice
Declare each teardown right after its setup, and trust LIFO ordering to unwind them correctly.
func pattern() {
print("acquire 1")
defer { print("release 1") }
print("acquire 2")
defer { print("release 2") }
print("work")
}
pattern()Quick Check
Test defer ordering.
Recap
Multiple defer blocks run in reverse (LIFO) order at scope exit: the last declared runs first. This naturally unwinds nested setup/teardown pairs in the correct order, even on early return.
func demo() {
defer { print("1") }
defer { print("2") }
print("body")
}
demo()Frequently asked questions
Is the “Multiple defer Blocks Ordering” lesson free?
Yes — the full text of “Multiple defer Blocks Ordering” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Multiple defer Blocks Ordering”?
Understand reverse execution order of defers. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Multiple defer Blocks Ordering” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- How defer Works
- Multiple defer Blocks Ordering
- defer for Resource Cleanup
- defer with Errors and Early Returns