为Swift中的自定义NSView提供指定的初始化程序

eri*_*icg 3 xcode cocoa nsview swift

我有一个自定义NSView类,看起来像:

class MyClass: NSView
{
    var myClassVar: NSColor
}
Run Code Online (Sandbox Code Playgroud)

当然,Xcode抱怨我的类没有初始化器,所以我需要覆盖指定的初始化器,以便初始化myClassVar.

我怎样才能做到这一点?

Sho*_*aib 9

试试这个;

       class MyClass: NSView
        {
            var myClassVar: NSColor! // the optional mark ! to be noticed.

            override init(frame frameRect: NSRect) {
                super.init(frame:frameRect);
            }

            required init(coder: NSCoder) {
                 super.init(coder: coder)
            }

            //or customized constructor/ init
            init(frame frameRect: NSRect, otherInfo:Int) {
                super.init(frame:frameRect);
                // other code
            }
        }
Run Code Online (Sandbox Code Playgroud)