Programmatically create an NSViewController without an XIB in Swift 3
<p>I'm trying to make a macOS app without using Interface Builder. My project builds and runs, but my primary view controller doesn't seem to be loading its view. That is, the <code>viewDidLoad()</code> method is not invoked. I'm using Xcode-beta 8.0 beta 6 (8S201h).</p>
<p>The Swift 3 documentation for <code>NSViewController</code> says this about the <code>view</code> property:</p>
<blockquote>
<p>If this property’s value is not already set when you access it, the view controller invokes the <code>loadView()</code> method. That method, in turn, sets the view from the nib file identified by the view controller’s <code>nibName</code> and <code>nibBundle</code> properties.</p>
<p>If you want to set a view controller’s view directly, set this property’s value immediately after creating the view controller.</p>
</blockquote>
<p>And for <code>viewDidLoad</code>:</p>
<blockquote>
<p>For a view controller created programmatically, this method is called immediately after the <code>loadView()</code> method completes.</p>
</blockquote>
<p>Finally, for <code>isViewLoaded</code>:</p>
<blockquote>
<p>A view controller employs lazy loading of its view: Immediately after a view controller is loaded into memory, the value of its <code>isViewLoaded</code> property is <code>false</code>. The value changes to <code>true</code> after the <code>loadView()</code> method returns and just before the system calls the <code>viewDidLoad()</code> method.</p>
</blockquote>
<p>So the documentation leads me to believe it is possible.</p>
<hr />
<p>My project looks like this:</p>
<pre><code> .
 ├── main.swift
 └── Classes
     ├── AppDelegate.swift
     └── Controllers   
         └── PrimaryController.swift
</code></pre>
<p><strong>main.swift</strong></p>
<pre><code>import Cocoa

let delegate = AppDelegate()
NSApplication.shared().delegate = delegate
NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
</code></pre>
<p><strong>AppDelegate.swift</strong></p>
<pre><code>import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let pc = PrimaryController(nibName: nil, bundle: nil)!
        print(pc.isViewLoaded)
        pc.view = NSView()
        print(pc.isViewLoaded)
    }
}
</code></pre>
<p><strong>PrimaryController.swift</strong></p>
<pre><code>import Cocoa

class PrimaryController: NSViewController {
    override func loadView() {
        print(&quot;PrimaryController.loadView&quot;)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print(&quot;PrimaryController.viewDidLoad&quot;)
    }
}
</code></pre>
<p>Building and running a project with the above yields this console output:</p>
<pre><code>false
true
</code></pre>
<p>That is, the view loads for the controller, but the <code>loadView</code> and <code>viewDidLoad</code> methods are never invoked.</p>
<p>What am I missing?</p>