0
0
Swiftprogramming~20 mins

Memory implications of captures in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Memory Capture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
A1\n2
B0\n1
C2\n3
D1\n1
Attempts:
2 left
💡 Hint
Think about how the closure captures the Counter instance and modifies it.
Predict Output
intermediate
2: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
ABoth Person and Apartment deinitialized messages printed
BOnly Person deinitialized message printed
CNo deinitialization messages printed (memory leak)
DOnly Apartment deinitialized message printed
Attempts:
2 left
💡 Hint
Consider how strong references between Person and Apartment affect memory.
🔧 Debug
advanced
2: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
Alazy var closure: () -> Void = { [strong self] in print(self.name) }
Blazy var closure: () -> Void = { [weak self] in print(self?.name ?? "No name") }
Clazy var closure: () -> Void = { print(self.name) }
Dlazy var closure: () -> Void = { [unowned self] in print(self.name) }
Attempts:
2 left
💡 Hint
Use capture lists to avoid strong reference cycles. Consider unowned vs weak.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in closure capture
Which option contains a syntax error in the closure capture list?
Alet closure = { [weak self] in print(self?.description) }
Blet closure = { [strong self] in print(self.description) }
Clet closure = { [weak self, unowned delegate] in print(self?.description) }
Dlet closure = { [unowned self] in print(self.description) }
Attempts:
2 left
💡 Hint
Check if the capture list keywords are valid Swift syntax.
🚀 Application
expert
3: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()
A1\n2
B0\n1
C1\n1
D2\n3
Attempts:
2 left
💡 Hint
Think about how the variable x is captured and modified by the closure.