0
votes

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.Picture of the code errors

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()
    }
}
1
try using SwiftUI.View instead of View.Mohammad Rahchamani
@MohammadRahchamani you may post this as an answer, it worked, thank youTadreik

1 Answers

2
votes

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.