简体   繁体   English

快速使用未实现的初始化程序'init()'

[英]Swift use of unimplemented initializer 'init()'

Part 1 第1部分

I am writing a Today Widget extension using NSExtensionPrincipalClass instead of storyboard. 我正在使用NSExtensionPrincipalClass而不是storyboard编写Today Widget扩展。 I implemented the following required init 我实现了以下必需的init

required init(coder aDecoder: NSCoder) {
    tableView = UITableView()
    cellIdentifier = "kCellIdentifier"

    super.init(coder: aDecoder)
}

However, when I run the project I get the following error 但是,当我运行该项目时,我收到以下错误

use of unimplemented initializer 'init()'

So, by putting this additional code in the project, it will fix the problem but it doesn't seem right to initialize variables in multiple places 因此,通过在项目中添加此附加代码,它将解决问题,但在多个位置初始化变量似乎不正确

override init() {
    tableView = UITableView()
    cellIdentifier = "kCellIdentifier"

    super.init()
}

Part 2 第2部分

So I answered part 1 of my question below but I still don't know why the following solution wouldn't work? 所以我回答了下面问题的第一部分,但我仍然不知道为什么以下解决方案不起作用?

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    cellIdentifier = "kCell"
    tableView = UITableView()
    super.init(nibName: nil, bundle: nil)
}

As per Swift's documentation 根据Swift的文档

“If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.” “如果您的子类提供了所有超类指定初始化器的实现 - 通过按照规则1继承它们,或者通过提供自定义实现作为其定义的一部分 - 那么它会自动继承所有超类便捷初始化器。”

In UIViewController, init(nibName:bundle:) is the only designated initialized so why am I not automatically inherits init() and init(coder:) ? 在UIViewController中, init(nibName:bundle:)是唯一指定的初始化,为什么我不自动继承init()init(coder:)

So here is the solution to the first part of my question after re-read swift's initializer chapter and look at UIViewController header file 所以这里是重新阅读swift的初始化器章节并查看UIViewController头文件后我的问题的第一部分的解决方案

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

override init() {
    cellIdentifier = "kCellIdentifier"
    tableView = UITableView()
    super.init(nibName: nil, bundle: nil)
}

One of the key to this solution is init(nibName:bundle:) which is a designated initializer which you must call whether you use storyboard or not. 这个解决方案的关键之一是init(nibName:bundle:) ,它是一个指定的初始化程序,无论你是否使用故事板,都必须调用它。

Keys take away: 钥匙带走:

  • If you don't provide a value for your variables when you declare them then you lose all the initializers inheritance from your superclass. 如果在声明变量时没有为变量提供值,则会丢失超类中的所有初始化器继承。 This means that you must provide your own init or override superclass initializers and call the appropriate designated initializer of your superclass. 这意味着您必须提供自己的init或override超类初始值设定项,并调用超类的相应指定初始值设定项。 Additionally, you must implement all required initializers. 此外,您必须实现所有必需的初始化程序。
  • By default in UIViewController, the convenience init will call init(nibName:bundle:) for you using nil as arguments. 默认情况下,在UIViewController中, convenience init将使用nil作为参数为您调用init(nibName:bundle:) So if you are not inherited the default init() method then it is your responsibility to override it and call init(nibName:bundle:) 因此,如果您没有继承默认的init()方法,那么您有责任覆盖它并调用init(nibName:bundle:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析/快速致命错误:使用未实现的初始化程序'init() - Parse/Swift fatal error: use of unimplemented initializer 'init() ios Swift 致命错误:使用未实现的初始化程序“init()” - ios Swift fatal error: use of unimplemented initializer 'init()' Swift Playground-对类使用未实现的初始化程序'init(size :)' - Swift Playground - Use of unimplemented initializer 'init(size:)' for class 致命错误:对 Swift 类使用未实现的初始化程序 'init()' - Fatal error: Use of unimplemented initializer 'init()' for class Swift Swift iOS-致命错误:使用未实现的初始值设定项'init(texture:color:size)' - Swift ios - fatal error: use of unimplemented initializer 'init(texture:color:size)' 致命错误:使用未实现的初始化程序Swift - Fatal error: use of unimplemented initializer Swift 致命错误:对类使用未实现的初始化程序 'init(coder:)' - Fatal error: use of unimplemented initializer 'init(coder:)' for class 致命错误:使用未实现的初始化程序'init(realm:schema :)' - Fatal Error: use of unimplemented initializer 'init(realm:schema:)' 调用Realm()。objects时使用未实现的初始化程序'init(realm:schema :)' - Use of unimplemented initializer 'init(realm:schema:)' when calling Realm().objects 实例化 UISegementedControl 的子类时,将未实现的初始化程序“init(frame:)”用于类 - Use of unimplemented initializer 'init(frame:)' for class when instantiating a subclass of UISegementedControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM