Closures & Capture basics
Understand closure syntax, shorthand args, trailing closure, capture lists, and escaping vs non-escaping.
Closures & Capture basics is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 3. 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 3 lessons in total.
Intro
This lesson introduces closures: self-contained blocks of code you can pass around. Explore syntax, shorthand args, trailing closure, capture lists, and escaping vs non-escaping.
Closure syntax
Closures look like lightweight functions. Syntax: { (params) -> Return in ... }.
let greet = { (name: String) -> String in
return "Hello, \\(name)"
}
print(greet("Ada"))Shorthand args
Swift provides shorthand args like $0, $1 to simplify closures.
let doubled = [1,2,3].map { $0 * 2 }
print(doubled)Trailing closure
If a closure is the last argument, you can write it outside parentheses: a trailing closure.
func operate(_ x: Int, _ y: Int, _ f: (Int,Int)->Int) -> Int {
return f(x,y)
}
let sum = operate(2,3) { $0 + $1 }
print(sum)Capture basics
Closures capture values from their surrounding scope, e.g., a running counter.
func makeCounter() -> () -> Int {
var count = 0
return {
count += 1
return count
}
}
let counter = makeCounter()
print(counter()) // 1
print(counter()) // 2Capture lists & escaping
Use capture lists like [weak self] to avoid retain cycles. Closures are non-escaping by default; mark @escaping if stored beyond call.
class Greeter {
var name = "Ada"
lazy var greet: () -> Void = { [weak self] in
print("Hello", self?.name ?? "?")
}
}
let g = Greeter()
g.greet()Capture list Q
Quick check: What is the role of a capture list?
Recap
Recap: Closures are inline blocks of code. Learn syntax, shorthand args, trailing closure, and control capture with lists. Escaping closures outlive the call; non-escaping are default.
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 “Closures & Capture basics” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Closures & Capture basics”, 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 3 lessons in total.
What will I learn in “Closures & Capture basics”?
Understand closure syntax, shorthand args, trailing closure, capture lists, and escaping vs non-escaping. 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 1 of 3.
How long does the “Closures & Capture basics” 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
- Closures & Capture basics
- Capture lists — weak/unowned & value capture
- Escaping vs non-escaping, autoclosures