I was writing some code similar to:
class ViewController : UIViewController {
var foo = "foo"
override func viewDidLoad() {
let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "", style: .default, handler: { _ in
print(foo)
}))
}
}
Xcode reports an error at print(foo) that I forgot to capture self, and suggests two fixes.:
print(self.foo), or;Add a capture list of
[self]to the closure:... handler: { [self] _ in ...
Now, I remember that before Xcode 12/Swift 5.3, it didn't use to do that. It either gives no fixes, or just the first one.
My question is, what's the difference between these two fixes? Do they somehow capture self in a different way?
The Language Guide seems to touch on this a little bit, but doesn't seem to say what the difference between them is.