FREE TRIAL: Accelerate your app development career with 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.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentViewController)


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:)


Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

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.