If you need to reference self
inside of a closure, it's good practice to pass it in as weak
or unowned
to prevent a retain cycle.
If I pass the function that belongs to self
directly, will it cause a retain cycle? Or does it need to be nested inside a closure to weakify self?
Passing directly
UIView.animateWithDuration(0.3,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.1,
options: .CurveEaseOut,
animations: self.view.layoutIfNeeded, // does this cause retain cycle?
completion: nil)
Wrapping in a closure
UIView.animateWithDuration(0.3,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.1,
options: .CurveEaseOut,
animations: { [unowned self] in
self.view.layoutIfNeeded()
},
completion: nil)