class MyClass {
func greeting() {
answer += "Hello, World!"
}
}
class Handler {
var closure: (() -> Void)?
let obj = MyClass()
func setupClosure() {
closure = {
self.obj.greeting()
}
}
deinit {
answer += "Handler is being deinitialized."
}
}
var answer: String = ""
func handlerDeinitialization() -> String {
var handler: Handler? = Handler()
handler!.setupClosure()
handler!.closure!()
handler = nil
return answer
}
print(handlerDeinitialization())
My expected answer is "Hellow, World! Handler is being deinitialized." How to fix this? I want to change one line of code only, not the whole one.
Rewrite:
(Note that I changed more than one line. But my answer is a huge clue towards how to do it by changing just one line.)