ios 初始化程序不会覆盖其超类中的指定初始化程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29031270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:11:50  来源:igfitidea点击:

Initializer does not override a designated initializer from its superclass

iosswiftuiviewcontrollerinitializer

提问by fulvio

So I've just upgraded to Xcode 6.3 Beta 3 and a lot of error(s) are appearing relating to the following:

所以我刚刚升级到 Xcode 6.3 Beta 3 并且出现了很多与以下相关的错误:

Initializer does not override a designated initializer from its superclass.

初始化程序不会覆盖其超类中的指定初始化程序。

override init() {
    super.init()
}

For example this is a UIButtonclass:

例如这是一个UIButton类:

class CustomButton: UIButton {

    var target: AnyObject!
    var selector: Selector!
    var action: (() -> Void)!

    override init() { // Initializer does not override a designated initializer from its superclass
        super.init() // Must call a designated initializer of the superclass 'UIButton'
    }

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

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
}

This is one of my UIViewControllerclasses:

这是我的UIViewController课程之一:

class CustomAlertView: UIViewController {

    required init(coder aDecoder: NSCoder) {
        fatalError("NSCoding not supported")
    }

    required override init() { // Initializer does not override a designated initializer from its superclass
        super.init() // Must call a designated initializer of the superclass 'UIViewController'
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
}

回答by Lluis Gerard

My solution is a quick fix, but I think is easier than what Apple purposes on the the Release Notes. For more information search for 19775924 http://adcdownload.apple.com//Developer_Tools/Xcode_6.3_beta_3/Xcode_6.3_beta_3_Release_Notes.pdfhere. What Apple says is that you create an Objective-C file and extend it (having to add it to the header files and all) and it's on "Known Issues in Xcode 6.3 beta 3", so I think is easy to do what I did:

我的解决方案是快速修复,但我认为比 Apple 在发行说明中的​​目的更容易。有关更多信息,请在此处搜索 19775924 http://adcdownload.apple.com//Developer_Tools/Xcode_6.3_beta_3/Xcode_6.3_beta_3_Release_Notes.pdf。Apple 所说的是您创建一个 Objective-C 文件并对其进行扩展(必须将其添加到头文件和所有文件中)并且它位于“Xcode 6.3 beta 3 中的已知问题”上,所以我认为很容易做到我所做的:

This is how I fixed it for UIButton:

这就是我修复它的方式UIButton

class CustomButton : UIButton {
    init() {
        super.init(frame: CGRectZero)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

And this is one of my ViewControllers (remove public if not needed):

这是我的 ViewControllers 之一(如果不需要,请删除 public):

public class GenericViewController: UIViewController {
    public init() {
        super.init(nibName: nil, bundle: nil)
    }

    required public init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

I don't use IB so I also have UIView, because I do separate the view from the viewController(remove public if not needed):

我不使用 IB,所以我也有UIView,因为我确实将视图与视图分开viewController(如果不需要,请删除公共):

public class GenericMenuView: UIView {
    public init() {
        super.init(frame: CGRectZero)
    }

    public required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

I need this specially in views because I have a setupViewsmethod that I override in all subclasses that is called on the init. And using AutoLayout I don't need any frames (so I don't override the init with the frame parameter).

我在视图中特别需要这个,因为我有一个setupViews方法可以在 init 调用的所有子类中覆盖。并且使用 AutoLayout 我不需要任何框架(所以我不会用 frame 参数覆盖 init )。

So it seems you have to drop override. Oh! and be sure to not call self.init()or the class is never initialized (and it crashes after some internal timeout).

所以看来你得放下了override。哦!并确保不要调用self.init()或该类永远不会被初始化(并且它会在一些内部超时后崩溃)。

回答by lostInTransit

As per Apple documentation here, what you are overriding is a convenience initializer. So for your initializer to work, you will have to change the method to

根据此处的Apple 文档,您要覆盖的是一个便利的初始值设定项。因此,要使您的初始化程序正常工作,您必须将方法更改为

override convenience init() {
    super.init()
}

You can either do that, or remove the initializer if you are not really using it except for calling the superclass initializer.

您可以这样做,或者如果您除了调用超类初始化程序之外没有真正使用它,则删除初始化程序。

回答by user3590685

I think this is way easier than it seems.

我认为这比看起来容易得多。

For an SKSpriteNode, I was doing this:

对于 SKSpriteNode,我是这样做的:

override init() {
    let texture = SKTexture(imageNamed: "bgTile")
    super.init(texture: texture, color: nil, size: texture.size())
}

The problem is init() is not the designated initializer for SKSpriteNode. So I just changed it to:

问题是 init() 不是 SKSpriteNode 的指定初始值设定项。所以我只是把它改成了:

override init(texture: SKTexture!, color: UIColor!, size: CGSize) {
    let texture = SKTexture(imageNamed: "bgTile")
    super.init(texture: texture, color: nil, size: texture.size())
}

Now it works fine.

现在它工作正常。

回答by Sanju

Solution for Error : Override init(coder aDecoder: NSCoder!) not working like expected - Swift

错误解决方案:覆盖 init(coder aDecoder: NSCoder!) 不按预期工作 - Swift

This works for me , Try this, Note: u must awake nib

这对我有用,试试这个,注意:你必须唤醒笔尖

override func awakeFromNib() {

     super.awakeFromNib()
    // Initialisation code

}

回答by fulvio

I recently figured this out and I'd like to explain what the problem was. Originally answered on the Apple Developer forums.

我最近想通了这一点,我想解释一下问题是什么。最初在Apple Developer 论坛上回答。

It seems Swift has changed the strategy for initializer dependency checking or for imporing initializers.

Swift 似乎改变了初始化器依赖检查或导入初始化器的策略。

Now if your initializers' are as shown, one way to deal with both Xcode 6.3 Beta 2 and Beta 3 is to remove all initializer definitions:

现在,如果您的初始化程序如图所示,处理 Xcode 6.3 Beta 2 和 Beta 3 的一种方法是删除所有初始化程序定义:

class CustomButton: UIButton {

    var target: AnyObject!
    var selector: Selector!
    var action: (() -> Void)!    
}

class CustomAlertView: UIViewController {

}

Without defining any designated initializers, classes inherit all initializers of their superclasses.

没有定义任何指定的初始值设定项,类继承其超类的所有初始值设定项。

A pretty easy fix, but a big gotcha that had me stumped for a while.

一个非常简单的修复,但是一个让我难住了一段时间的大问题。