我正在尝试NSTextFieldCell使用以下代码在Swift(Xcode Beta 5)中创建自定义子类:
class CustomHighlightTextFieldCell : NSTextFieldCell {
required init(coder aCoder: NSCoder!) {
super.init(coder: aCoder)
}
init(imageCell anImage: NSImage!) {
super.init(imageCell: anImage)
}
init(textCell aString: String!) {
super.init(textCell: aString)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我在第2和第3个init()声明中收到编译错误:
/Users/Craig/projects/.../CustomHighlightTextFieldCell:8:40: Invalid redeclaration of 'init(imageCell:)'
/Users/Craig/projects/.../CustomHighlightTextFieldCell.swift:17:5: 'init(imageCell:)' previously declared here
/Users/Craig/projects/.../CustomHighlightTextFieldCell:7:39: Invalid redeclaration of 'init(textCell:)'
/Users/Craig/projects/.../CustomHighlightTextFieldCell.swift:21:5: 'init(textCell:)' previously declared here
Run Code Online (Sandbox Code Playgroud)
虽然这里有一些奇怪的编译器错误(我得到通常的"SourceKitService终止,编辑器功能暂时限制."消息),似乎我在我的方法覆盖中遗漏了一些东西 - 但我不知道是什么.
我假设命名参数,或者至少是参数类型,表明这里有三种不同的init()方法,但显然我错过了这个谜题的关键部分.
编辑:如果我添加override到第二和第三种init()方法,我有一个单独的问题:
required init(coder aCoder: NSCoder!) {
super.init(coder: aCoder)
}
override init(imageCell anImage: NSImage!) {
super.init(imageCell: anImage)
}
override init(textCell aString: String!) {
super.init(textCell: aString)
}
Run Code Online (Sandbox Code Playgroud)
这个新问题(仅通过添加两个override关键字来调用)似乎与原始问题几乎相矛盾.
/Users/Craig/projects/.../CustomHighlightTextFieldCell.swift:17:14: Initializer does not override a designated initializer from its superclass
/Users/Craig/projects/.../CustomHighlightTextFieldCell.swift:21:14: Initializer does not override a designated initializer from its superclass
Run Code Online (Sandbox Code Playgroud)
看起来你的声明(使用覆盖)应该足够了,但是,它们似乎也需要@objc声明.这有效:
class CustomHighlightTextFieldCell : NSTextFieldCell {
required init(coder aCoder: NSCoder!) {
super.init(coder: aCoder)
}
@objc(initImageCell:)
override init(imageCell anImage: NSImage!) {
super.init(imageCell: anImage)
}
@objc(initTextCell:)
override init(textCell aString: String!) {
super.init(textCell: aString)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3117 次 |
| 最近记录: |