致命错误:对类使用未实现的初始化程序“init(coder:)"

Fatal error: use of unimplemented initializer #39;init(coder:)#39; for class(致命错误:对类使用未实现的初始化程序“init(coder:))
本文介绍了致命错误:对类使用未实现的初始化程序“init(coder:)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定用 Swift 继续我剩下的项目.当我将自定义类(UIViewcontroller 的子类)添加到我的故事板视图控制器并加载项目时,应用程序突然崩溃并出现以下错误:

I decided to continue my remaining project with Swift. When I add the custom class (subclass of UIViewcontroller) to my storyboard view controller and load the project, the app crashes suddenly with the following error:

致命错误:对类使用未实现的初始化程序init(coder:)"

fatal error: use of unimplemented initializer 'init(coder:)' for class

这是代码:

import UIKit

class TestViewController: UIViewController {

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

    override func viewDidLoad() {
        super.viewDidLoad()
              // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
}

请提出建议

推荐答案

问题

这是由于目标 UIViewController 上没有初始化器 init?(coder aDecoder: NSCoder) 造成的.该方法是必需的,因为从 UIStoryboard 实例化 UIViewController 会调用它.

Issue

This is caused by the absence of the initializer init?(coder aDecoder: NSCoder) on the target UIViewController. That method is required because instantiating a UIViewController from a UIStoryboard calls it.

要了解我们如何从 UIStoryboard 初始化 UIViewController,请查看 这里

To see how we initialize a UIViewController from a UIStoryboard, please take a look here

因为 Objective-C 自动继承了所有必需的 UIViewController 初始化器.

Because Objective-C automatically inherits all the required UIViewController initializers.

Swift 出于安全考虑,默认情况下不继承初始化器.但是如果所有属性都有值(或可选)并且子类没有定义任何指定的初始化器,它将继承父类的所有初始化器.

Swift by default does not inherit the initializers due to safety. But it will inherit all the initializers from the superclass if all the properties have a value (or optional) and the subclass has not defined any designated initializers.

在目标UIViewController

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


2.第二种方法

删除目标 UIViewController 上的 init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) 将从超类继承所有必需的初始化程序为 Dave Wood 指出了他的答案


2. Second method

Removing init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) on your target UIViewController will inherit all of the required initializers from the superclass as Dave Wood pointed on his answer below

这篇关于致命错误:对类使用未实现的初始化程序“init(coder:)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

The Google Mobile Ads SDK was initialized without AppMeasurement(Google 移动广告 SDK 在没有 AppMeasurement 的情况下进行了初始化)
No JNI_OnLoad found in ... skipping init(在 ... 中未找到 JNI_OnLoad 正在跳过初始化)
No JNI_OnLoad found skipping init gt; Application shutdown(没有找到跳过初始化的 JNI_OnLoad 应用程序关闭)
Error in Swift class: Property not initialized at super.init call(Swift 类中的错误:在 super.init 调用时未初始化属性)
Conditional Binding: if let error – Initializer for conditional binding must have Optional type(条件绑定:if let error - 条件绑定的初始化程序必须具有可选类型)
Safe to signal semaphore before deinitialization just in case?(在取消初始化之前发出信号量信号安全吗?以防万一?)