NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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 MadMachine

SPONSORED Want to try Swift on microcontrollers? MadMachine provides ways to interact with the physical world in a Swift way. Join us and have fun!

Get it now

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.