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
Missing argument for parameter 'coder' in call in Swift
提问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
SecondViewController
fromUIViewController
- Simply to initialize any new
SecondViewController
asSecondViewController()
- 继承我
SecondViewController
的UIViewController
- 只需将任何新初始化
SecondViewController
为SecondViewController()
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 UIViewController
does initialization.
"required init(coder aDecoder: NSCoder)"
如果您创建UIViewController
. 也是如此"super.init(nibName: nil, bundle: nil)"
,因为这是UIViewController
初始化的方式。