The following pattern happens a lot in iOS apps:
class MyViewController: UIViewController {
let myModel = MyModel()
override func viewDidLoad() {
super.viewDidLoad()
myModel.foo() { [***] in
// use self here
}
}
}
class MyModel {
public func foo(complete: () -> Void) {
// do something
complete()
}
}
The consensus is to use either [unowned self]
or [weak self]
in place of the [***]
, unowned when you can guarantee that self will not be nil at the time of completion and weak when you're not sure the reference will still be valid.
What I don't understand is why I would ever risk using unowned, maybe I'm sure that right now the reference will never be nil, but that can change in the future. I could also have overlooked an edge-case, mistakes happen. I could just as easily always use weak, and put a guard at the top of the closure to be able to use self without a ! or ?.
What is the use of unowned? Is it faster than weak+guard? Is it syntactic sugar? It seems to go against Swift's philosophy of protecting the developer against common mistakes that can cause crashes.