How to pass a local function to another object during init?
I did the below, but got \'self\' captured by a closure before all members were initialized
First, your property drawViewWrapper
seems to be useless here since you can access it later with self.rootView
and second, you should use [unowned self]
to avoid capturing 'self' and creating a weak reference to it instead (and then avoid a retain-cycle).
So I would do something like this instead:
class ContextViewController: UIHostingController {
init(drawView: CustomDrawView) {
let drawViewWrapper = ContentView(
drawView: drawView,
dismiss: { [unowned self] in
self.dismiss(animated: true)
})
super.init(rootView: drawViewWrapper)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And then if you need to access it later, you can simply use self.rootView
.