类不实现其超类的必需成员 [英] Class does not implement its superclass's required members

查看:70
本文介绍了类不实现其超类的必需成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我今天更新到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.

错误说明:


Class'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.

你可以采取的另一个选择很好的方法是将方法实现为一个方便的初始化,如下所示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆