简体   繁体   English

Swift类中的错误:super.init调用时未初始化属性-如何初始化需要在其初始值设定项参数中使用self的属性

[英]Error in Swift class: Property not initialized at super.init call - How to initialize properties which need use of self in their initializer parameter

I am overriding a UITableViewController in swift, in which I have two required variables which are initialized by using a weak reference of self since these are used to implement UITableViewDataSource protocol and need self reference to use its tableView property 我正在快速重写UITableViewController ,在其中我有两个必需的变量,这些变量通过使用selfweak引用进行初始化,因为它们用于实现UITableViewDataSource协议,并且需要使用self引用才能使用其tableView属性

class VideosListViewController: UITableViewController {

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.datasourceOfflineVideos = ASDataSource(tableViewController: self)
    self.datasourceOnlineVideos = ASDataSource(tableViewController: self)
  }

  // MARK: - Variables
  var datasourceOnlineVideos:ASDataSource
  var datasourceOfflineVideos:ASDataSource
  }

Now the problem is as it is giving me error Property not initialized at super.init call which is expected as explained here: Error in Swift class: Property not initialized at super.init call . 现在的问题是,它给了我错误error Property not initialized at super.init call ,如下所示: Swift类中的错误:在super.init调用中未初始化的属性

Swift's compiler performs four helpful safety-checks to make sure that two-phase initialization is completed without error Swift的编译器执行四项有用的安全检查,以确保两阶段初始化完成且没有错误

Safety check 1 A designated initializer must ensure that all of the “properties introduced by its class are initialized before it delegates up to a superclass initializer. 安全检查1指定的初始值设定项在将其委托给超类初始值设定项之前,必须确保初始化其类引入的所有属性。

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. 摘自:Apple Inc.“快速编程语言”。iBooks。 https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11 https://itunes.apple.com/cn/book/swift-programming-language/id881256329?mt=11

So my question is: 所以我的问题是:

If an instance variable in swift class needs self reference to be initialized like ASDataSource type variables here, how can I do that?? 如果swift类中的实例变量需要像此处的ASDataSource类型变量一样初始化self引用,该怎么办?

Since swift doesn't allow me call the super.init() before initiazing all the instance variables And also doesn't allow me to user self within the initializer in the init() before calling super.init() 由于swift不允许我在初始化所有实例变量之前调用super.init() ,并且也不允许我在调用super.init()之前在init()的初始化程序中使用用户self

Currently I am using optional variable(s) to resolve the issue. 目前,我正在使用可选变量来解决此问题。 But for learning purpose I want to know how to do that. 但是出于学习目的,我想知道如何去做。 Thanks in advance :) 提前致谢 :)

You just have to invert the order super.init/properties in your initializer: 您只需要在初始化程序中反转super.init / properties的顺序即可:

 required init(coder aDecoder: NSCoder) {
    self.datasourceOfflineVideos = ASDataSource(tableViewController: self)
    self.datasourceOnlineVideos = ASDataSource(tableViewController: self)
    super.init(coder: aDecoder)
  }

instance properties comes first, then the superclass initializer can be invoked. 首先提供实例属性,然后才能调用超类初始化程序。 But that's not possible in your case, because you are referencing self . 但这在您的情况下是不可能的,因为您引用的是self

The workaround in this case is to make the properties implicitly unwrapped optionals: 在这种情况下,解决方法是使属性隐式展开为可选:

var datasourceOnlineVideos:ASDataSource!
var datasourceOfflineVideos:ASDataSource!

Since optionals don't need to be initialized, you can safely initialize them after the super.init method has been called. 由于不需要初始化可选选项,因此可以在调用super.init方法之后安全地对其进行初始化。 Being implicitly unwrapped, you use them as any other non optional property. 被隐式展开时,可以将它们用作其他任何非可选属性。

This pattern is used by Apple in several classes, including UIViewController : when you add an outlet from IB, the component property is always declared as implicitly unwrapped. Apple在多个类(包括UIViewController都使用了这种模式:从IB添加插座时,组件属性始终被声明为隐式展开。 That's because the control itself is not instantiated in the initializer, but at a later stage. 这是因为控件本身不是在初始化程序中实例化的,而是在以后的阶段中实例化的。

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

相关问题 Swift super.init() - 属性未在 super.init 调用中初始化 - Swift super.init() - Property not initialized at super.init call swift 属性未在 super.init 调用中初始化 - swift Property not initialized at super.init call Swift:属性未在super.init调用时初始化 - Swift: Property not initialized at super.init call Swift中的错误:属性'self.circle1'在super.init调用时没有初始化? - Error in Swift: property 'self.circle1' not initialized at super.init call? Swift Class中的自定义init,在“隐式生成的super.init调用”错误中“未初始化属性”? - Custom init in Swift Class giving 'property not initialized at implicitly generated super.init call' error? 在super.init调用之前使用“ self”,并且在super.init调用中未初始化属性 - 'self' used before super.init call and property not initialized at super.init call 属性 'self.readCount' 未在 super.init 调用时初始化 - Property 'self.readCount' not initialized at super.init call 属性'self。*'未在super.init调用中初始化 - Property 'self.*' not initialized at super.init call super.init调用未初始化属性“ self.menuViewController” - Property 'self.menuViewController' not initialized at super.init call 升级到Swift 3后,未在super.init调用中初始化属性'self.delegate' - Property 'self.delegate' not initialized at super.init call after upgrading to Swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM