If I have a closure passed to a function like this:
someFunctionWithTrailingClosure { [weak self] in
anotherFunctionWithTrailingClosure { [weak self] in
self?.doSomething()
}
}
If I declare self as [weak self]
in someFunctionWithTrailingClosure
's capture list without redeclaring it as weak
again in the capture list of anotherFunctionWithTrailingClosure
self
is already becoming an Optional
type but is it also becoming a weak
reference as well?
Thanks!
The
[weak self]
inanotherFunctionWithTrailingClosure
is not needed.You can empirically test this:
And then:
If you do this, you will see that
doSomething
is never called and thatdeinit
is called beforeanotherFunctionWithTrailingClosure
calls its closure.That having been said, I might still be inclined to use the
[weak self]
syntax onanotherFunctionWithTrailingClosure
to make my intent explicit.