初始化程序不会覆盖其超类中的指定初始化程序 [英] Initializer does not override a designated initializer from its superclass

查看:40
本文介绍了初始化程序不会覆盖其超类中的指定初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Initializer 不会覆盖其超类中的指定初始化器.

Initializer does not override a designated initializer from its superclass.

override init() {
    super.init()
}

例如这是一个 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)
    }
}

这是我的 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)
    }
}

推荐答案

我的解决方案是一个快速修复,但我认为比 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 中的已知问题"上,所以我认为很容易做到我所做的:

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.pdf here. 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:

这是我为 UIButton 修复它的方法:

This is how I fixed it for UIButton:

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

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

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

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

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")
    }
}

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

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

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

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

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

I need this specially in views because I have a setupViews method 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).

所以看来你必须放弃override.哦!并且一定不要调用 self.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).

这篇关于初始化程序不会覆盖其超类中的指定初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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