Reputation: 947
I created a new SwiftUI View in my Xcode project that only used UIKit. I am trying to create new screens for the app in SwiftUI and then just embed them in ViewControllers. Xcode autogenerated the struct and the preview in the SwiftUI file. But then almost every line gets an error, almost as if Xcode is having trouble importing SwiftUI into the file.
I have: properly set up the window scene in SceneDelegate.swift, removed the storyboard key from info.plist, turned on previews in the project build settings, make sure my project is targeting iOS 13.0, cleaned derived data.
My setup in sceneDelegate with SwiftUI imported:
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ProfilePage())
self.window = window
window.makeKeyAndVisible()
}
// ERROR: Generic class 'UIHostingController' requires that 'ProfilePage' conform to 'View'
My code:
import SwiftUI
struct ProfilePage: View {
var body: some View {
Text("Hello, World!")
}
}
struct ProfilePage_Previews: PreviewProvider {
static var previews: some View {
ProfilePage()
}
}
Upvotes: 0
Views: 792
Reputation: 5220
as you can see in the error message, XCode is referring to UIView
instead of SwiftUI.View
.
you should replace View
with SwiftUI.View
to fix the problem.
Upvotes: 4