Swift Academy · Lesson

Closure Capture Lists and [weak self]

Using capture lists to prevent closures from strongly capturing objects.

Lesson 4 of 412 steps

Closure Capture Lists and [weak self] is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. 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 4 lessons in total.

Welcome

Closures capture variables from their surrounding scope. Capture lists (`[weak self]`, `[unowned self]`, `[value]`) control exactly how those captures are made.

Default Strong Capture

```swift var name = "Alice" let greet = { print("Hello, \(name)") } name = "Bob" greet() // "Hello, Bob" — closure sees current value ``` By default, closures capture *references* to variables.

Capture List Basics

```swift var count = 10 let snapshot = { [count] in print(count) } // captures value count = 99 snapshot() // 10 — snapshot of count at capture time ``` Listing a value type in `[]` copies the value at the moment the closure is created.

[weak self] Pattern

```swift class API { func fetch(completion: @escaping () -> Void) { ... } } class VC { func load() { api.fetch { [weak self] in guard let self else { return } // self might be nil by now self.updateUI() } } } ```

guard let self with Extended Lifetime

Swift 5.3+: rebind `self` inside the guard without shadow: ```swift { [weak self] in guard let self else { return } // 'self' is now strong within this block doA() doB() doC() } ```

[unowned self] When Safe

```swift button.addTarget(self, action: #selector(tap), for: .touchUpInside) // If the button's closure is guaranteed not to outlive self: let action: () -> Void = { [unowned self] in self.handleTap() } ```

Capturing Multiple Values

```swift let base = 100 let multiplier = 2 let compute = { [base, multiplier] in base * multiplier } print(compute()) // 200 ``` Capture multiple values in the same list, separated by commas.

Capture List with Named Bindings

```swift let myService = service // rename in capture list: let handler = { [service = myService] in service.process() } ``` Useful for capturing a specific snapshot under a new name.

Escaping vs Non-Escaping

• `@escaping` closure may be stored and called after the function returns → risk of cycles • Non-escaping closure is called before the function returns → no cycle risk ```swift func apply(_ f: () -> Void) { f() } // non-escaping, no [weak] needed func later(_ f: @escaping () -> Void) { queue.async { f() } } // escaping ```

Debugging Capture Issues

If you suspect a cycle: 1. Add `deinit { print("deallocated") }` to the class 2. Navigate away or set instance to nil 3. If deinit doesn't print → cycle exists Then add `[weak self]` to closures in that class.

Quick Check

What does `[weak self]` in a capture list do?

Recap

Key takeaways: • Closures capture references by default • Capture list `[value]` copies value types at creation time • `[weak self]` — no retain, auto-nil, use `guard let self` • `[unowned self]` — no retain, not nil-safe • `@escaping` closures need capture lists; non-escaping ones usually don't Course complete! Next: functional programming in Swift.
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 “Closure Capture Lists and [weak self]” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Closure Capture Lists and [weak self]”, 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 4 lessons in total.

What will I learn in “Closure Capture Lists and [weak self]”?

Using capture lists to prevent closures from strongly capturing objects. 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 4 of 4.

How long does the “Closure Capture Lists and [weak self]” 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. How ARC Counts References
  2. Retain Cycles: Causes and Detection
  3. weak and unowned References
  4. Closure Capture Lists and [weak self]
← Back to Swift Academy