Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSWindowController in Swift. Subclassing and initializing with Nib

In a test Swift project, I am subclassing NSWindowController. My NSWindowController subclass is designed to work with a particular Nib file. It is desirable, then, that when my window controller is initialized, the nib file is automatically loaded by the window controller instance. In Objective-C, this was achieved by doing:

@implementation MyWindowController

- (id)init {
    self = [super initWithWindowNibName:"MyWindowNib"]
    if (self) {
        // whatever
    }
    return self
}

@end

Now, in Swift this is not possible: init() cannot call super.init(windowNibName:), because the later is declared not as a designated initializer, but as a convenience one by NSWindowController.

How can this be done in Swift? I don't see a strightforward way of doing it.

P.S.: I have seen other questions regarding this topic, but, as long as I've been able to understand, the solutions all point to initialize the Window Controller by calling init(windowNibName:). Please note that this is not the desired beheaviour. The Window Controller should be initialized with init(), and it should be the Window Controller itself who "picks up" its Nib file and loads it.

like image 407
George Avatar asked Aug 16 '14 13:08

George


1 Answers

If you use the init() just to call super.init(windowNibName:), you could instead just override the windowNibName variable.

override var windowNibName: String  {
    get {
        return "MyWindowNib"
    }
}

Then there should be no need to mess with the initializers.

like image 159
nrms Avatar answered Oct 20 '22 04:10

nrms