ios 为什么我不能在 Swift 中调用 UIViewController 上的默认 super.init()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24095037/
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
Why can't I call the default super.init() on UIViewController in Swift?
提问by SirRupertIII
I am not using a UIViewController
from a storyboard and I want to have a custom init
function where I pass in an NSManagedObjectID
of some object. I just want to call super.init()
like I have in Objective-C. Like this:
我没有使用UIViewController
故事板中的 a ,我想要一个自定义init
函数,我可以在其中传入NSManagedObjectID
某个对象的 。我只想像super.init()
在 Objective-C 中那样调用。像这样:
init(objectId: NSManagedObjectID) {
super.init()
}
But I get the following compiler error:
但我收到以下编译器错误:
Must call designated initializer of the superclass UIViewController
必须调用超类 UIViewController 的指定构造器
Can I simply not do this anymore?
我可以不再这样做了吗?
回答by occulus
The designated initialiser for UIViewController
is initWithNibName:bundle:
. You should be calling that instead.
的指定初始值设定项UIViewController
是initWithNibName:bundle:
。你应该打电话给那个。
If you don't have a nib, pass in nil
for the nibName (bundle is optional too). Then you could construct a custom view in loadView
or by adding subviews to self.view
in viewDidLoad
, same as you used to.
如果您没有笔尖,请输入笔尖nil
名称(捆绑也是可选的)。然后,您可以在 in 中loadView
或通过向self.view
in添加子视图来构建自定义视图viewDidLoad
,就像以前一样。
回答by Klaas
Another nice solution is to declare your new initializer as a convenience
initializer as follows:
另一个不错的解决方案是将您的新初始化程序声明为convenience
初始化程序,如下所示:
convenience init( objectId : NSManagedObjectID ) {
self.init()
// ... store or user your objectId
}
If you declare no designated initializers in your subclass at all, they are inherited automatically and you are able to use self.init()
within your convenience initializer.
如果您在子类中根本没有声明任何指定的构造器,它们将被自动继承,并且您可以self.init()
在您的便利构造器中使用它们。
In case of UIViewController the default init method will call init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!)
with nil
for both arguments (Command-Click on UIViewController will give you that info).
在UIViewController中的情况下,默认的init()方法将调用init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!)
具有nil
两个参数(命令点击UIViewController中会给你的信息)。
TL;TR: If you prefer to programmatically work with UIViewController
s here is a complete working example that adds a new initializer with a custom argument:
TL;TR:如果您更喜欢以编程方式使用UIViewController
s 这里是一个完整的工作示例,它添加了一个带有自定义参数的新初始化程序:
class MyCustomViewController: UIViewController {
var myString: String = ""
convenience init( myString: String ) {
self.init()
self.myString = myString
}
}
回答by NcNc
Update: add the link
更新:添加链接
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621359-init
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621359-init
According to the documentation for iOS, the designated initialiser for UIViewController is initWithNibName: bundle:
.
根据 iOS 的文档, UIViewController 的指定初始化程序是initWithNibName: bundle:
.
If you subclass UIViewController, you must call the super implementation of this method, even if you aren't using a NIB.
如果你继承 UIViewController,你必须调用这个方法的超级实现,即使你没有使用 NIB。
You can do it as follows:
你可以这样做:
init(objectId : NSManagedObjectID) {
super.init(nibName: (xib's name or nil'), bundle: nil)
// other code...
}
or
或者
Declare a new initializer as a convenience initializer:
声明一个新的初始化器作为便利初始化器:
convenience init( objectId : NSManagedObjectID ) {
self.init()
// other code...
}
}
回答by Vyacheslav
To improve the occulus'sanswer:
改进occulus 的答案:
init() {
super.init(nibName: nil, bundle: nil)
}