本文介绍了致命错误:在课堂上使用未实现的初始化程序'init(coder :)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定用Swift语言继续我的剩余项目。当我在storyboard viewcontroller中添加自定义类(.swift类,哪个子类到UIViewcontroller)并加载项目时,应用程序突然崩溃,后面有错误

I decided to continue my remaining project with Swift-language.When I added the custom class (.swift class which sub class to UIViewcontroller) in my storyboard viewcontroller and loaded the project the app crash suddenly with following error:

这是一段代码:

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 上的code> init?(编码器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上手动实现 init?(编码器aDecoder:NSCoder)

Manually implementing init?(coder aDecoder: NSCoder) on the target UIViewController

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






2。第二种方法



在你的目标上删除 init(nibName nibNameOrNil:String ?, bundle nibBundleOrNil:NSBundle?) c $ c> UIViewController 将继承超类中所有必需的初始值设定项, 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 :)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:00