Shall we always use [unowned self] inside closure in Swift
In WWDC 2014 session 403 Intermediate Swift and transcript, there was the following slide
The speaker said in that case, if we don't use [unowned self]
there, it will be a memory leak. Does it mean we should always use [unowned self]
inside closure?
On line 64 of ViewController.swift of the Swift Weather app, I don't use [unowned self]
. But I update the UI by using some @IBOutlet
s like self.temperature
and self.loadingIndicator
. It may be OK because all @IBOutlet
s I defined are weak
. But for safety, should we always use [unowned self]
?
class TempNotifier {
var onChange: (Int) -> Void = {_ in }
var currentTemp = 72
init() {
onChange = { [unowned self] temp in
self.currentTemp = temp
}
}
}