I am using Eureka to set up my table view. I have a section with a header:
Section() { [weak self] in
guard let strongSelf = self else { return }
var header = HeaderFooterView<MyView>(.nibFile(name: "MView", bundle: nil))
header.onSetupView = strongSelf.setUpHeader(view:section:)
$0.header = header
...
}
private func setUpHeader(view: MyView, section: Section) {
// content here doesn't seem to make a difference.
}
For some reason it always sets up a retain cycle on the line header.onSetupView = strongSelf.setUpHeader(view:section:). If I move the code from the setUpHeader(view: MyView, section: Section) function into a block like this, there is no retain cycle:
header.onSetupView = { [weak self] view, section in
}
Why is this??
This line creates a strong reference to
strongSelf, which is a strong reference toself, so transitively that creates a strong reference toselfin theonSetupViewclosure.Saying it another way, what you've written here is the same as:
And since
strongSelfis a strong reference toself, that's the same thing as strong reference toself:And just one more way to say it:
selfcannot be deallocated beforestrongSelfis, because thenstrongSelfwould be an invalid reference.