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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24036393/
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 00:24:20  来源:igfitidea点击:

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

iosmacosuiviewcontrollerswift

提问by Pratik Bhiyani

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:

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

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

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

This is a code:

这是一个代码:

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.
    }
    */
}

Please suggest something

请提出一些建议

回答by E-Riddie

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 UIViewControllerfrom a UIStoryboardcalls it.

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

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

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

Why is this not a problem with Objective-C?

为什么这不是 Objective-C 的问题?

Because Objective-Cautomatically inherits all the required UIViewControllerinitializers.

因为Objective-C 会自动继承所有必需的UIViewController初始化程序。

Why doesn't Swift automatically inherit the initializers?

为什么 Swift 不自动继承初始化器?

Swiftby 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.

由于安全原因,Swift默认不继承初始值设定项。但是如果所有属性都有一个值(或可选)并且子类没有定义任何指定的初始化器,它将继承超类的所有初始化器。



Solution

解决方案

1. First method

1.第一种方法

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

init?(coder aDecoder: NSCoder)在目标上手动实施UIViewController

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


2. Second method

2. 第二种方法

Removing init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)on your target UIViewControllerwill inherit all of the required initializers from the superclass as Dave Woodpointed on his answerbelow

删除init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)您的目标UIViewController将从超类继承所有必需的初始值设定项,因为Dave Wood在下面的回答中指出



回答by Dave Wood

Another option besides @3r1d's is to instead remove the following init method from your class:

除了@3r1d 的另一个选项是从您的类中删除以下 init 方法:

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

Including that init method, prevents the sub class from inheriting the init(coder aDecoder: NSCoder!)from its super class. By not including it, your class will inherit both.

包括该 init 方法,可防止子类init(coder aDecoder: NSCoder!)从其超类继承。通过不包含它,您的类将继承两者。

Note: See WWDC 2014 Session 403 "Intermediate Swift" at about the 33:50 mark for more details.

注意:有关更多详细信息,请参阅 33:50 左右的 WWDC 2014 Session 403“Intermediate Swift”。

回答by Nick Yap

For people having the same issue with swift UICollectionViewCells, add the code that @3r1d suggested to your custom UICollectionViewCellclass and not to the View Controller:

对于 swift 有同样问题的人UICollectionViewCells,将@3r1d 建议的代码添加到您的自定义UICollectionViewCell类而不是视图控制器:

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

回答by MXV

For those needing the code in Swift:

对于那些需要 Swift 代码的人:

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

[Edit] This was for an older version of Swift. Possibly doesn't work anymore.

[编辑] 这是针对旧版本的 Swift。可能不再起作用了。

回答by Lance Samaria

I had this problem in a programmatic collectionView cell and even though the op is asking about a vc I still landed on this question when searching for an answer. For me the issue was I did have

我在程序化 collectionView 单元格中遇到了这个问题,即使操作员询问了 vc,我在搜索答案时仍然遇到了这个问题。对我来说,问题是我确实有

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

implemented so the top answer didn't work. What I didn't have in the cell was the initializer:

已实施,因此最佳答案不起作用。我在单元格中没有的是初始化程序:

// my programmatic cell was missing this
override init(frame: CGRect) {
    super.init(frame: frame)
}

Once I added it the error went away

一旦我添加它,错误就消失了

回答by hasancan85

Rather than adding some methods for making internal mechanism work fine, i would go with defining my attributes as @lazy and initialise them right in the class scope.

我不会添加一些方法来使内部机制正常工作,而是将我的属性定义为 @lazy 并在类范围内初始化它们。