类未实现其超类的必需成员 [英] Class does not implement its superclass's required members
问题描述
所以我今天更新到 Xcode 6 beta 5,发现我在 Apple 类的几乎所有子类中都收到了错误.
So I updated to Xcode 6 beta 5 today and noticed I received errors in nearly all of my subclasses of Apple's classes.
错误状态:
类x"未实现其超类所需的成员
Class 'x' does not implement its superclass's required members
这是我选择的一个例子,因为这个类目前非常轻量级,所以很容易发布.
Here is one example I picked because this class is currently pretty lightweight so it will be easy to post.
class InfoBar: SKSpriteNode { //Error message here
let team: Team
let healthBar: SKSpriteNode
init(team: Team, size: CGSize) {
self.team = team
if self.team == Team.TeamGood {
healthBar = SKSpriteNode(color: UIColor.greenColor(), size:size)
}
else {
healthBar = SKSpriteNode(color: UIColor.redColor(), size:size)
}
super.init(texture:nil, color: UIColor.darkGrayColor(), size: size)
self.addChild(healthBar)
}
}
所以我的问题是,为什么我会收到此错误,我该如何解决?我没有执行什么?我正在调用指定的初始化程序.
So my question is, why am I receiving this error, and how can I fix it? What is it that I am not implementing? I'm calling a designated initializer.
推荐答案
来自开发者论坛上的 Apple 员工:
From an Apple employee on the Developer Forums:
"向编译器和构建的程序声明你真正不想与 NSCoding 兼容就是做这样的事情:"
"A way to declare to the compiler and the built program that you really don't want to be NSCoding-compatible is to do something like this:"
required init(coder: NSCoder) {
fatalError("NSCoding not supported")
}
如果您知道自己不想遵守 NSCoding,这是一个选项.我的很多 SpriteKit 代码都采用了这种方法,因为我知道我不会从情节提要中加载它.
If you know you don't want to be NSCoding compliant, this is an option. I've taken this approach with a lot of my SpriteKit code, as I know I won't be loading it from a storyboard.
您可以采用的另一个效果很好的选项是将方法实现为便利 init,如下所示:
Another option you can take which works rather well is to implement the method as a convenience init, like so:
convenience required init(coder: NSCoder) {
self.init(stringParam: "", intParam: 5)
}
注意 self
中对初始化程序的调用.这允许您只需要对参数使用虚拟值,而不是所有非可选属性,同时避免引发致命错误.
Note the call to an initializer in self
. This allows you to only have to use dummy values for the parameters, as opposed to all non-optional properties, while avoiding throwing a fatal error.
第三个选项当然是在调用 super 时实现该方法,并初始化所有非可选属性.如果对象是从故事板加载的视图,您应该采用这种方法:
The third option of course is to implement the method while calling super, and initialize all of your non-optional properties. You should take this approach if the object is a view being loaded from a storyboard:
required init(coder aDecoder: NSCoder!) {
foo = "some string"
bar = 9001
super.init(coder: aDecoder)
}
这篇关于类未实现其超类的必需成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!