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]inanotherFunctionWithTrailingClosureis not needed.You can empirically test this:
And then:
If you do this, you will see that
doSomethingis never called and thatdeinitis called beforeanotherFunctionWithTrailingClosurecalls its closure.That having been said, I might still be inclined to use the
[weak self]syntax onanotherFunctionWithTrailingClosureto make my intent explicit.