使用 Xcode 11 GM Seed 编写一些 SwiftUI 代码,我遇到了一个我不明白的 Swift 错误。
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>) {
}
}
此报告:
'UIViewControllerRepresentable' requires the types 'some View' and 'Never' be equivalent
最佳答案
我错过了 ViewController 和 View 之间的分离。错误是说 View Controller 不能有返回 View 的主体。
这个有效:
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>) {
}
}
然后实例化它:
let viewController = UIHostingController<MainViewController>(rootView:MainViewController())
关于swift - UIViewControllerRepresentable 要求类型 'some View' 和 'Never' 是等效的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57905693/