The code below creates a strong reference cycle.
class Car {
let doors = 10
lazy var closure : () -> Int = {
return 55
}
deinit {
print("de-initialized")
}
}
func f1() {
print("f1")
var c1 : Car? = Car()
c1?.closure = {
return (c1?.doors ?? 99) + 1
}
//c1 = nil
}
f1()
Questions:
- Why is the instance of Car not de-initialized though c1 goes out of scope ?
- However when c1 is assigned nil, Car instance is de-initialized, why is that ?
- In the code, c1?.closure is assigned a closure, but the closure is not invoked, so why does it create a strong reference cycle ?