bibin-jaimon / InterviewKit-iOS

5 stars 0 forks source link

Swift: predict code output #6

Open bibin-jaimon opened 10 months ago

bibin-jaimon commented 10 months ago
class Capture {
    var value = 10
    deinit {
        print("dinit")
    }
}

var instance: Capture? = Capture()

let closureExpression: () -> Capture? = { [unowned instance] in
    return instance
}

let result: Capture? = closureExpression()

instance = nil

instance?.value = 100

print(result?.value)
bibin-jaimon commented 8 months ago

The output will be 10 because even though the instance is nil, the result variable is having strong reference to the Capture instance.