Yes -- here is another type of implicit capture that I've seen:
class KittenPartyViewController: UIViewController {
func hideKittens(animated: Bool) {
func changeOpacity() {
kittensView.alpha = 0
}
func hide(_: Bool) {
kittensView.isHidden = true // implicitly captures strong self
}
if animated {
UIView.animate(withDuration: 2, animations: changeOpacity, completion: hide)
} else {
changeOpacity()
hide(true)
}
}
}
The implicit strong reference to self
in the local hide()
function escapes when passed as the completion handler for UIView.animate()
. It might be nice to have a warning in this case too, but that seems like a much more difficult problem to solve. (In practice, I've never seen a retain cycle created this way either.)
For the case of passing a method as a closure, I think the benefits are balanced with the difficulty of implementing the check. (This is just a guess, I don't actually know if it would be hard to implement.)