0Pricing
Swift Academy · Lesson

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 — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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())  // 2

Capture 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.

Frequently asked questions

Is the “Closures & Capture basics” lesson free?

Yes — the full text of “Closures & Capture basics” is free to read here on the web, and the Swift Academy course includes 3 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 “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; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

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

  1. Closures & Capture basics
  2. Capture lists — weak/unowned & value capture
  3. Escaping vs non-escaping, autoclosures
← Back to Swift Academy