0Pricing
Swift Academy · Lesson

Capture lists — weak/unowned & value capture

Use capture lists to control how closures capture references and values: [weak self], unowned, and value snapshots to avoid retain cycles and subtle bugs.

Capture lists — weak/unowned & value capture is a free Swift Academy lesson on CoddyKit — lesson 2 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.

Why capture lists?

Goal: Control what and how a closure captures. Use [weak self] or unowned to avoid retain cycles, and capture value snapshots when needed.

Strong capture = cycle risk

Escaping/stored closures retain captured values. Capturing self strongly here may create a retain cycle.

class Owner {
    var name = "Owner"
    var onDone: (() -> Void)?

    func start() {
        // Strong capture of self can cause a cycle if onDone is stored:
        onDone = {
            print("Done by", self.name) // self captured strongly
        }
    }
}
var o: Owner? = Owner()
o?.start()
// If nothing nils onDone or o, both can keep each other alive.

Weak capture pattern

Use [weak self] to break the cycle. self is optional inside; unwrap with guard let or optional chaining.

class Owner2 {
    var name = "Owner2"
    var onDone: (() -> Void)?

    func start() {
        onDone = { [weak self] in
            guard let self = self else { return }  // safely unwrap optional self
            print("Done by", self.name)
        }
    }
}
let o2 = Owner2()
o2.start()
o2.onDone?()

unowned (use carefully)

unowned assumes self outlives the closure (non-optional). Use only when you're sure it cannot be nil at call time.

class Owner3 {
    var onDone: (() -> Void)?
    func work() {
        onDone = { [unowned self] in
            // self must be alive when this runs; otherwise crash
            print("Owner3 still alive")
        }
    }
}
let o3 = Owner3()
o3.work()
o3.onDone?()

Value snapshot idea

Capture lists can snapshot values (e.g., [n = count]). Without a list, let constants inside are still captured at creation.

var count = 0
let makeLogger: () -> () -> Void = {
    let snapshot = count     // captured by value at creation
    return {
        print("snapshot =", snapshot, "current =", count)
    }
}
count = 10
let logger = makeLogger()
count = 20
logger()   // snapshot = 10, current = 20

Capture list value binding

Use a capture list to create a named value snapshot (e.g., [initial = total]) at closure creation time.

var total = 5
let closure = { [initial = total] in
    print("initial:", initial, " now:", total)
}
total = 9
closure()   // initial uses captured value 5

weak self reason

Quick check: Why add [weak self] to a stored/escaping closure?

Recap

Recap: Use [weak self] to break cycles (self becomes optional), unowned only when lifetime is guaranteed, and capture value snapshots with capture-list bindings.

Frequently asked questions

Is the “Capture lists — weak/unowned & value capture” lesson free?

Yes — the full text of “Capture lists — weak/unowned & value capture” 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 “Capture lists — weak/unowned & value capture”?

Use capture lists to control how closures capture references and values: [weak self], unowned, and value snapshots to avoid retain cycles and subtle bugs. 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 3, so you can start here or from the beginning and move at your own pace.

How long does the “Capture lists — weak/unowned & value capture” 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