在Swift中初始化NSWindowController对象的正确方法是什么?

时间:2016-01-07 22:51:30

标签: swift cocoa

尽我所能,我无法在这里得到语法。我做错了什么?

class MainWindowController: NSWindowController {

    let myThing: Thing

    override convenience init ( window: NSWindow! ) {
        myThing = Thing()
        self.init( windowNibName: "nibText" )
    }

    ...
}

非常感谢提前!

1 个答案:

答案 0 :(得分:3)

  1. myThing = Thing()可以放置在声明属性的位置
  2. 您应致电super.init
  3. 您还需要实施init?(coder: NSCoder),Xcode会很乐意为此插入模板
  4. 您很可能需要另一个可以加载笔尖的初始化程序:
  5. 这样的事情:

    class MainWindowController: NSWindowController {
    
        // the type of myThing will be inferred by the compiler
        let myThing = Thing()
    
        init() {
            super.init(window: nil)
            // load the contents of the nib, and set the owner as self, which connects the oultlets
            NSBundle.mainBundle().loadNibNamed("nibText", owner: self, topLevelObjects: nil)
        }
    
        // you might no longer need this intializer
        override init(window: NSWindow!) {
            super.init( window: window )
        }
    
       // ...
    }