Challenge - 5 Problems
Swift Memory Capture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of closure capturing a class instance
What will be printed when this Swift code runs?
Swift
class Counter { var count = 0 func increment() { count += 1 } } func makeIncrementer() -> () -> Void { let counter = Counter() return { counter.increment() print(counter.count) } } let incrementer = makeIncrementer() incrementer() incrementer()
Attempts:
2 left
💡 Hint
Think about how the closure captures the Counter instance and modifies it.
✗ Incorrect
The closure captures the same Counter instance, so each call increments the count. The first call prints 1, the second prints 2.
❓ Predict Output
intermediate2:00remaining
Memory leak due to strong reference cycle
What error or behavior will this Swift code cause?
Swift
class Person { let name: String var apartment: Apartment? init(name: String) { self.name = name } deinit { print("Person \(name) is being deinitialized") } } class Apartment { let unit: String var tenant: Person? init(unit: String) { self.unit = unit } deinit { print("Apartment \(unit) is being deinitialized") } } var john: Person? = Person(name: "John") var unit4A: Apartment? = Apartment(unit: "4A") john!.apartment = unit4A unit4A!.tenant = john john = nil unit4A = nil
Attempts:
2 left
💡 Hint
Consider how strong references between Person and Apartment affect memory.
✗ Incorrect
Person and Apartment hold strong references to each other, creating a cycle. Neither is deinitialized when variables are set to nil, causing a memory leak.
🔧 Debug
advanced2:00remaining
Fixing a retain cycle with capture lists
This Swift closure causes a memory leak by capturing self strongly. Which option correctly fixes the leak?
Swift
class ViewController { var name = "Main" lazy var closure: () -> Void = { print(self.name) } deinit { print("ViewController deinitialized") } } var vc: ViewController? = ViewController() vc?.closure() vc = nil
Attempts:
2 left
💡 Hint
Use capture lists to avoid strong reference cycles. Consider unowned vs weak.
✗ Incorrect
Using [unowned self] avoids a strong reference cycle and safely accesses self without optional unwrapping. [weak self] requires optional handling. [strong self] is invalid syntax.
📝 Syntax
advanced2:00remaining
Identify the syntax error in closure capture
Which option contains a syntax error in the closure capture list?
Attempts:
2 left
💡 Hint
Check if the capture list keywords are valid Swift syntax.
✗ Incorrect
There is no 'strong' keyword allowed in capture lists. Only 'weak' and 'unowned' are valid. Option B is invalid syntax.
🚀 Application
expert3:00remaining
Analyzing memory behavior of nested closures
Consider this Swift code with nested closures capturing variables. What is the output when calling outer() twice?
Swift
func outer() -> () -> Void { var x = 0 let inner = { x += 1 print(x) } return inner } let closure = outer() closure() closure()
Attempts:
2 left
💡 Hint
Think about how the variable x is captured and modified by the closure.
✗ Incorrect
The closure captures the variable x by reference, so each call increments x. The first call prints 1, the second prints 2.