简体   繁体   English

Swift:在从初始化程序返回之前没有在所有路径上调用“super.init”?

[英]Swift: 'super.init' isn't called on all paths before returning from initializer?

I am getting this error on the last brace of a init in a class of mine.我在我的一类中的 init 的最后一个大括号中收到此错误。 The class looks something like the following (I market the spot where error happens):该类类似于以下内容(我将发生错误的位置作为市场营销):

class RecordingViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {

    let cameraButton:UIButton?
    let camPreview:UIView?

    init (cameraButton: UIButton!, camPreview: UIView!) {
        self.cameraButton = cameraButton
        self.camPreview = camPreview

    } //get error here

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

    //do a bunch of other stuff
}

I have looked here and here for a solution but both seem like solutions that are either really bad or that are too specific to that question, thus they have not work for me.我已经在这里这里寻找解决方案,但两者似乎都是非常糟糕的解决方案或针对该问题过于具体的解决方案,因此它们对我不起作用。

I was hoping for a solution to my problem done in such a way that it can help me understand why this error is happening.我希望以这样一种方式解决我的问题,它可以帮助我理解为什么会发生这个错误。

Since you inherit from UIViewController, you should call super.init right after you set the variables in your init function由于您从 UIViewController 继承,因此您应该在init函数中设置变量后立即调用super.init

When you inherit a class and implement a new init function or override its own init function you should (almost) always call super.init.当你继承一个类并实现一个新的 init 函数或覆盖它自己的 init 函数时,你应该(几乎)总是调用 super.init。 Let's take your example, you inherited from UIViewController.让我们以您的示例为例,您继承自 UIViewController。 UIViewController has a few init functions that you can use to initialize a view controller. UIViewController 有一些初始化函数,你可以用它们来初始化一个视图控制器。 if you don't call super.init, all the code inside those functions will not get called and possibly the view controller won't get initialized.如果你不调用 super.init,这些函数中的所有代码都不会被调用,并且视图控制器可能不会被初始化。

Anyway, this piece of code should work for you:无论如何,这段代码应该适合你:

class ViewController: UIViewController {

    var button: UIButton?

    init(button: UIButton) {
        self.button = button
        super.init(nibName: nil, bundle: nil)
    }

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

}

Here is what I found on Swift Programming Language:这是我在 Swift 编程语言中发现的内容:

In the first phase, each stored property is assigned an initial value by the class that introduced it.在第一阶段,每个存储的属性都由引入它的类分配一个初始值。 Once the initial state for every stored property has been determined, the second phase begins, and each class is given the opportunity to customize its stored properties further before the new instance is considered ready for use.一旦确定了每个存储属性的初始状态,第二阶段就开始了,每个类都有机会在新实例准备好使用之前进一步自定义其存储属性。

A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer.一个指定的初始化器必须确保它的类引入的所有属性在它委托给一个超类初始化器之前都被初始化。

Hope this can explain that question.希望这可以解释这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从初始化程序返回之前,并非在所有路径上都调用“ super.init” - 'super.init' isn't called on all paths before returning from initializer 解决“从初始化快速返回之前未在所有路径上调用Super.init的问题”。 - Problems getting around 'Super.init isn't called on all paths before returning from initializer swift.' 在从初始化程序错误返回之前,不会在所有路径上调用Super.init - Super.init isn't called on all paths before returning from initializer Error 从初始化程序返回之前不会调用Super.init - Super.init isn't called before returning from initializer 在从初始化程序返回之前,不会在所有路径上调用super init - super init isn't called on all paths before returning from initializer 在从初始化程序返回之前,不会在所有路径上调用“self.init” - 'self.init' isn't called on all paths before returning from initializer json.swift 错误是 Self.init 未在委托初始化程序的所有路径上调用 - json.swift error is Self.init isn't called on all paths in delegating initializer swift3中无法调用super.init() - super.init() couldn't be called in swift3 RealmSwift初始值设定项:在super.init调用之前自行使用 - RealmSwift initializer: self used before super.init call 在Swift中调用NSObject子类的初始化程序中的super.init() - Calling super.init() in initializer of NSObject subclass in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM