Swift Diary #6: Window Controller Initialization

I’m working on UI code — and the below took me a half hour and quite a bit of frustration to figure out, so I thought I’d better write it up.

Here’s the deal: I want my window controllers (and view controllers too) to know about their nib name, but the outside world should not know about their nib name.

Here’s how I handle this in Objective-C: the NSWindowController’s subclass has an init method that references the nib name.

- (instancetype)init {
  return [self initWithWindowNibName:​@"SomeNibName"];

To instantiate the window controller from elsewhere, I just do [SomeWindowController new]. (Could be alloc/init, but I like the fewer characters this way.)

Here’s how to do it in Swift:

convenience init() {
  self.init(windowNibName: "SomeNibName")

And then I can instantiate the window controller via SomeWindowController(), and all’s well.

Now that I have the answer, it looks so simple. Many Bothans died, etc.

PS Yes, I’m using storyboards, except for the few things that should be nibs.

01 Aug 2015