Hi,
I'm trying to create a ViewController that extends UIHostingController and host an AttachmentView there. Also, I would like to pass an environment object to the AttachmentView, but I get a compile error because the type of rootView does not match what I specify in UIHostingController.
How can I get this code to compile?
Thanks!
When you show some code, you should better show it as text using Code Block. With easily testable code shown, more readers would be involved solving your issue.
(Of course, images are very useful for some additional info.)
Modifiers like environmentObject(_:)
would make an instance of a certain private type, like ModifiedContent<AttachmentView, _EnvironmentKeyWritingModifier<AttachmentViewModel?>>
(may change in versions of SwiftUI).
So, you cannot specify the generic parameter of UIHostingController
as AttachmentView
, neither cannot use ModifiedContent<...>
explicitly as Swift compiler hides details of some View
.
It seems you have not many options:
- Avoid subclassing
UIHostingController
, that requires the generic parameter specified. - Avoid any modifiers.
With the latter, you can write something like this:
class AttachmentViewController: UIHostingController<AttachmentView> {
let model = AttachmentViewModel()
init() {
let attachmentView = AttachmentView(model: model)
super.init(rootView: attachmentView)
}
@objc required dynamic init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
struct AttachmentView: View {
var model: AttachmentViewModel
var body: some View {
InnerAttachemntView()
.environmentObject(model)
}
}