UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to set view controller entry point in appdelegate

Forums > macOS

Hi everyone,

I am trying to programmatically set "viewController" as an entry point in appdelegate. However, I keep getting this error message "Generic class 'NSHostingView' requires that 'NSStoryboard' conform to 'View'" . The error is happening with this line of code: "window.contentView = NSHostingView(rootView: contentViewController)". Any feedback is appreciated, thank you for your time.

func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        let contentViewController = NSStoryboard(name: "Main.storyboard", bundle: nil)

        // Create the window and set the content view. 
        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)

        window.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentViewController)
        window.makeKeyAndOrderFront(nil)
    }

3      

NSHostingView is used to embed a SwiftUI View object into an AppKit view hierarchy. You are trying to embed an AppKit ViewController. Can't do that.

If you are using SwiftUI, you won't be instantiating views from storyboards. You need to assign a View object to NSHostingView(rootView:)

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.