Multiple defer Blocks Ordering
Understand reverse execution order of defers.
Multiple defer Blocks Ordering is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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()Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Multiple defer Blocks Ordering” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Multiple defer Blocks Ordering”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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