Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing NSWindowController in Swift and init(windowNibName)

I am trying to start a new document based Cocoa project in Swift and want to create a subclass of NSWindowController (as recommended in Apple's guides on document based apps). In ObjC you would make an instance of an NSWindowController subclass sending the initWithWindowNibName: message, which was implemented accordingly, calling the superclasses method.

In Swift init(windowNibName) is only available as an convenience initializer, the designated initializer of NSWindowController is init(window) which obviously wants me to pass in a window.

I cannot call super.init(windowNibName) from my subclass, because it is not the designated initializer, so I obviously have to implement convenience init(windowNibName), which in turn needs to call self.init(window). But if all I have is my nib file, how do I access the nib file's window to send to that initializer?

like image 289
Martin Avatar asked Jun 14 '14 13:06

Martin


2 Answers

Instead of overriding any of the init methods you can simply override the windowNibName property and return a hardcoded string. This allows you to call the basic vanilla init method to create the window controller.

class WindowController: NSWindowController {      override var windowNibName: String! {         return "NameOfNib"     } }  let windowController = WindowController() 

I prefer this over calling let windowController = WindowController(windowNibName: "NameOfNib") as the name of the nib is an implementation detail that should be fully encapsulated within the window controller class and never exposed outside (and it's just plain easier to call WindowController()).

If you want to add additional parameters to the init method do the following:

  • In your custom init method call super.init(window: nil). This will get NSWindowController to init with the windowNibName property.
  • Override the required init(coder: NSCoder) method to either configure your object or simply call fatalError() to prohibit its use (albiet at runtime).
class WindowController: NSWindowController {      var test: Bool      override var windowNibName: String! {         return "NameOfNib"     }      init(test: Bool) {         self.test = test         super.init(window: nil) // Call this to get NSWindowController to init with the windowNibName property     }      // Override this as required per the class spec     required init?(coder: NSCoder) {         fatalError("init(coder:) has not been implemented. Use init()")          // OR          self.test = false         super.init(coder: coder)     } }  let windowController = WindowController(test: true) 
like image 116
ospr Avatar answered Oct 05 '22 08:10

ospr


You need to override either all three designated initializers of NSWindowController (init(), init(window) and init(coder)), or none of them, in which case your subclass will automatically inherit init(windowNibName) and all others convenience initializers and you will be able to construct it using superclass's convenience initializer:

// this overrides none of designated initializers class MyWindowController: NSWindowController {     override func windowDidLoad() {         super.windowDidLoad()     } }  // this one overrides all of them // // Awkwardly enough, I see only two initializers  // when viewing `NSWindowController` source from Xcode,  // but I have to also override `init()` to make these rules apply. // Seems like a bug. class MyWindowController: NSWindowController {     init()     {         super.init()     }      init(window: NSWindow!)     {         super.init(window: window)     }      init(coder: NSCoder!)     {         super.init(coder: coder)     }      override func windowDidLoad() {         super.windowDidLoad()     } }  // this will work with either of the above let mwc: MyWindowController! = MyWindowController(windowNibName: "MyWindow") 

This is covered by "Initialization / Automatic Initializer Inheritance" in the language guide:

However, superclass initializers are automatically inherited if certain conditions are met. In practice, this means that you do not need to write initializer overrides in many common scenarios, and can inherit your superclass initializers with minimal effort whenever it is safe to do so.

Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:

Rule 1 If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

Rule 2 If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.

like image 45
hamstergene Avatar answered Oct 05 '22 10:10

hamstergene