NSDocumentController basics

Xcode 10, macOS 13.5, Swift 4.2

Without wanting to delve too deep into the NSDocumentController class:

It’s part of the document architecture, and if your app uses more than one NSDocument subclass, you will need to override the following method:

override func makeUntitledDocument(ofType typeName: String) throws -> NSDocument {
switch typeName {
case "ThingB":
return ThingB()
default:
return ThingA()
}
}

NSDocumentController has odd behaviour I haven’t really seen elsewhere; it uses a shared instance, but the way to get in first with your subclass is to instantiate an instance of the class. I find that the best place is

func applicationWillFinishLaunching(_ notification: Notification) {
MyDocumentController.init()
}

applicationDidFinishLaunching is too late (if you try it there, NSDocumentController.shared already exists, and yours will just deinit immediately.

And now the application will automagically use MyDocumentController.