使用闭包时,我通常会在捕获列表中添加[weak self]
,然后对self
进行空值检查:
使用闭包时,我通常会在捕获列表中添加[weak self]
,然后对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)
}