0
0
Swiftprogramming~30 mins

Closures causing retain cycles in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Closures Causing Retain Cycles in Swift
📖 Scenario: Imagine you are building a simple app where a Person has a closure property that prints their name. You want to understand how closures can cause retain cycles, which means the Person and the closure keep each other alive and never get removed from memory.
🎯 Goal: You will create a Person class with a closure property that captures self. Then, you will add a configuration variable to control whether to use a capture list to avoid retain cycles. Finally, you will observe the output to understand when retain cycles happen.
📋 What You'll Learn
Create a Person class with a name property and a closure property called printNameClosure.
Add a Boolean variable useCaptureList to decide if the closure captures self weakly.
Write the closure inside the Person initializer using useCaptureList to avoid or cause retain cycles.
Print messages when the Person instance is deinitialized to observe retain cycles.
💡 Why This Matters
🌍 Real World
Understanding retain cycles is important when building iOS apps to prevent memory leaks that slow down or crash apps.
💼 Career
Swift developers must manage memory carefully. Knowing how closures capture references helps write efficient and safe code.
Progress0 / 4 steps
1
Create the Person class with name and closure
Create a class called Person with a name property of type String. Add a closure property called printNameClosure of type () -> Void. Initialize printNameClosure as an empty closure in the class.
Swift
Need a hint?

Define the class with name and printNameClosure. Initialize printNameClosure as an empty closure.

2
Add a configuration variable to control capture behavior
Add a Boolean variable called useCaptureList to the Person class. Initialize it in the init method with a default value of false.
Swift
Need a hint?

Add useCaptureList as a property and initialize it in the constructor with a default false.

3
Set the closure with or without weak capture of self
Inside the init method, assign printNameClosure a closure that prints the name. Use useCaptureList to decide if the closure captures self weakly with [weak self] or strongly without a capture list.
Swift
Need a hint?

Use [weak self] in the closure capture list when useCaptureList is true. Otherwise, capture self strongly.

4
Create and test Person instances to observe retain cycles
Create a function called testPerson that creates a Person instance named person with name "Alice" and useCaptureList set to false. Call person.printNameClosure(). Then set person to nil. Print "Person instance is set to nil" after. Add a deinit method in Person that prints "Person \(name) is being deinitialized". Run testPerson() and observe the output.
Swift
Need a hint?

Create a testPerson function that creates and uses a Person instance, then sets it to nil. Add a deinit method to observe deinitialization.