How defer Works
Run code at scope exit regardless of path.
How defer Works is a free Swift Academy lesson on CoddyKit — lesson 1 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.
What defer Does
A defer block schedules code to run when the current scope is about to exit, no matter how it exits.
func demo() {
defer { print("cleanup") }
print("work")
}
demo()Runs at Scope Exit
The deferred code runs after the rest of the scope finishes, just before control leaves it.
func greet() {
defer { print("goodbye") }
print("hello")
}
greet()Deferred After Normal Flow
Even with multiple statements, the defer body runs last.
func process() {
defer { print("3 deferred") }
print("1 start")
print("2 middle")
}
process()defer Inside a Loop
A defer inside a loop body runs at the end of each iteration.
for i in 1...3 {
defer { print("end of iteration", i) }
print("start of iteration", i)
}defer Inside an if
The scope can be any block. A defer in an if runs when that if block ends.
func check(_ flag: Bool) {
if flag {
defer { print("leaving if block") }
print("inside if")
}
print("after if")
}
check(true)Capturing Current Values
The deferred block captures variables by reference, so it sees their values at the moment it runs.
func counterDemo() {
var count = 0
defer { print("final count:", count) }
count = 5
}
counterDemo()defer Does Not Delay Work
defer does not run asynchronously. It runs synchronously at scope exit, before the function returns to its caller.
func order() {
defer { print("B") }
print("A")
}
order()
print("C")Common Use: Pairing Setup/Teardown
Place teardown right next to setup so the two stay visually and logically together.
func session() {
print("open session")
defer { print("close session") }
print("do work")
}
session()defer in a do Block
A defer inside a do block runs when that block exits.
func run() {
do {
defer { print("do block cleanup") }
print("inside do")
}
print("after do")
}
run()Multiple Statements in defer
A defer block can contain several statements; all run together at scope exit.
func multi() {
defer {
print("step 1 of cleanup")
print("step 2 of cleanup")
}
print("main work")
}
multi()Why Use defer
defer guarantees cleanup happens regardless of how the scope ends, reducing the chance of forgetting it on some path.
func guaranteed() {
defer { print("always runs") }
print("body")
}
guaranteed()Quick Check
Test defer timing.
Recap
defer schedules code to run at the end of the current scope, regardless of how it exits. It runs synchronously, last in the scope, and is ideal for keeping teardown next to setup.
func demo() {
defer { print("last") }
print("first")
}
demo()Frequently asked questions
Is the “How defer Works” lesson free?
Yes — the full text of “How defer Works” 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 “How defer Works”?
Run code at scope exit regardless of path. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “How defer Works” 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.