0Pricing
Swift Academy · Lesson

Closure Capture Lists and [weak self]

Using capture lists to prevent closures from strongly capturing objects.

Closure Capture Lists and [weak self] is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Closure Capture Lists and [weak self]” lesson free?

Yes — the full text of “Closure Capture Lists and [weak self]” is free to read here on the web, and the Swift Academy course includes 4 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 “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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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