xcode Swift 调用中缺少参数“coder”的参数

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

Missing argument for parameter 'coder' in call in Swift

iosxcodeswift

提问by aquajach

class SecondViewController:UIViewController {
  required init(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)
    //Custom logic here
  }
}

Quite a newbie question:

很菜鸟的问题:

a view controller(SecondViewController), inherent from UIViewController needs a designated init function like above.

UIViewController 固有的视图控制器(SecondViewController)需要像上面一样指定的 init 函数。

In this case, how should I call it, given I am not sure what the value for "coder" should be? I used to call the controller as: SecondViewController(), but it gives:

在这种情况下,我应该如何称呼它,因为我不确定“编码器”的值应该是什么?我曾经将控制器称为:SecondViewController(),但它给出:

Missing argument for parameter 'coder' in call

I understand coder parameter has to be provided, but want to ask what its value comes from.

我知道必须提供编码器参数,但想问问它的值来自什么。

回答by aquajach

Thanks for the answers from @Chackle. Finally the solution I figured out is below.

感谢@Chackle 的回答。最后,我想出的解决方案如下。

What I want:

我想要的是:

  • Inherit my SecondViewControllerfrom UIViewController
  • Simply to initialize any new SecondViewControlleras SecondViewController()
  • 继承我SecondViewControllerUIViewController
  • 只需将任何新初始化SecondViewControllerSecondViewController()

Solution:

解决方案:

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

    init() {
        super.init(nibName: nil, bundle: nil)
        //Do whatever you want here
    }

"required init(coder aDecoder: NSCoder)"is a must if you create a subclass of UIViewController. And so is "super.init(nibName: nil, bundle: nil)", because it is the way how UIViewControllerdoes initialization.

"required init(coder aDecoder: NSCoder)"如果您创建UIViewController. 也是如此"super.init(nibName: nil, bundle: nil)",因为这是UIViewController初始化的方式。