尝试对NSTextView进行子类化:
class MYTextView : NSTextView {
init(frame frameRect: NSRect) {
super.init(frame: frameRect)
setup()
}
}
我遇到了这个错误:
Must call a designated initializer of the superclass 'NSTextView'
,出现在这一行:super.init(frame: frameRect)
。根据文档,
Convenience initializers must call another initializer available in the same class.
。详见下面的“Initializer Chaining”章节:
https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_286
但是对于NSTextViews,我只得到了三个指定初始化器:super.init(frame:, textContainer:)
、super.init(coder: coder)
和 super.inti()
。其中init(frame:)
会进行一些设置,我不想自己实现。有没有办法使用超类的方便初始化器?
init()
和init(coder:)
需要被覆盖以继承方便的初始化。 - Alex Marchantinit(coder:)
不是指定的,它实际上来自一个协议(NSCoding
)。 - Sulthaninit(frame:,textContainer:)
会出现错误 https://gist.github.com/alexmarchant/3fd270471920af517908。加入init()
仍然有错误 https://gist.github.com/alexmarchant/78dd4654115cad7c67b7。但是使用所有三个就可以了 https://gist.github.com/alexmarchant/d3dbe45563386db18ec6。 - Alex Marchant