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.

Lesson 2 of 38 steps

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

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.

Free to start

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 “Capture lists — weak/unowned & value capture” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Capture lists — weak/unowned & value capture”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

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