【发布时间】:2014-12-27 04:58:15
【问题描述】:
对于闭包,我通常将[weak self] 附加到我的捕获列表中,然后对自身进行空值检查:
func myInstanceMethod()
{
let myClosure =
{
[weak self] (result : Bool) in
if let this = self
{
this.anotherInstanceMethod()
}
}
functionExpectingClosure(myClosure)
}
如果我使用嵌套函数代替闭包,我如何对self 执行空检查(或者检查是否必要......或者使用这样的嵌套函数甚至是一种好习惯) 即
func myInstanceMethod()
{
func nestedFunction(result : Bool)
{
anotherInstanceMethod()
}
functionExpectingClosure(nestedFunction)
}
【问题讨论】:
-
从 rintaro 的答案中学习我写了一个更强大的答案here
标签: swift memory-management weak-references nested-function capture-list