Writing some SwiftUI code using the Xcode 11 GM Seed, and I've run into a Swift error I don't understand.
struct MainViewController: View {
var body: some View {
VStack {
Text("Hello World!")
}
}
}
extension MainViewController : UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<MainViewController>) -> UINavigationController {
return UINavigationController()
}
func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<MainViewController>) {
}
}
This reports:
'UIViewControllerRepresentable' requires the types 'some View' and 'Never' be equivalent
I was missing the separation between ViewController and View. The error was saying that a view controller can't have a body that returns a view.
This works:
struct MainView : View {
var body: some View {
VStack {
Text("Hello World!")
}
}
}
struct MainViewController : UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<MainViewController>) -> UIHostingController<MainView> {
return UIHostingController(rootView: MainView())
}
func updateUIViewController(_ uiViewController: UIHostingController<MainView>, context: UIViewControllerRepresentableContext<MainViewController>) {
}
}
And then to instantiate it:
let viewController = UIHostingController<MainViewController>(rootView:MainViewController())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With