如果您需要在闭包内引用self
,最好将其作为weak
或unowned
传递以防止循环引用。
如果我直接传递属于self
的函数,会导致循环引用吗?还是需要将其嵌套在闭包内以使self
弱化?
直接传递
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)
使用闭包进行封装
UIView.animateWithDuration(0.3,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.1,
options: .CurveEaseOut,
animations: { [unowned self] in
self.view.layoutIfNeeded()
},
completion: nil)