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(Swift 类中的错误:在 super.init 调用时未初始化属性 - 如何初始化需要在其初始化参数中使用 self 的属性) - IT屋-程序员软件开发技术分

本文介绍了Swift 类中的错误:在 super.init 调用时未初始化属性 - 如何初始化需要在其初始化参数中使用 self 的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 swift 中覆盖 UITableViewController,其中我有两个必需的变量,它们通过使用 selfweak 引用进行初始化,因为这些用于实现 UITableViewDataSource 协议,需要 self 引用才能使用其 tableView 属性

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

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
  }

现在的问题是它给了我错误 Property not initialized at super.init call 预期如下所述:Swift 类中的错误:在 super.init 调用时属性未初始化.

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.

Swift 的编译器会执行四项有用的安全检查,以确保两阶段初始化完成时不会出错

Swift’s compiler performs four helpful safety-checks to make sure that two-phase initialization is completed without error

安全检查 1 指定的初始化程序必须确保其类引入的所有属性在它委托给超类初始化程序之前都已初始化.

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.

摘自:Apple Inc. Swift 编程语言".电子书.https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

Excerpt From: Apple Inc. "The Swift Programming Language." iBooks. https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

所以我的问题是:

如果 swift 类中的实例变量需要像 ASDataSource 类型变量一样在此处初始化 self 引用,我该怎么做??

If an instance variable in swift class needs self reference to be initialized like ASDataSource type variables here, how can I do that??

由于 swift 不允许我在初始化所有实例变量之前调用 super.init() 并且也不允许我使用 self在调用 super.init()

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()

目前我正在使用可选变量来解决问题.但出于学习目的,我想知道如何做到这一点.在此先感谢:)

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 :)

推荐答案

你只需要在你的初始化程序中反转 super.init/properties 的顺序:

You just have to invert the order super.init/properties in your initializer:

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

首先出现实例属性,然后可以调用超类初始化程序.但这在您的情况下是不可能的,因为您引用的是 self.

instance properties comes first, then the superclass initializer can be invoked. But that's not possible in your case, because you are referencing self.

在这种情况下,解决方法是使属性隐式展开可选:

The workaround in this case is to make the properties implicitly unwrapped optionals:

var datasourceOnlineVideos:ASDataSource!
var datasourceOfflineVideos:ASDataSource!

由于不需要初始化可选项,您可以在调用 super.init 方法后安全地初始化它们.隐式展开后,您可以将它们用作任何其他非可选属性.

Since optionals don't need to be initialized, you can safely initialize them after the super.init method has been called. Being implicitly unwrapped, you use them as any other non optional property.

Apple 在几个类中使用了这种模式,包括 UIViewController:当您从 IB 添加一个插座时,组件属性总是被声明为隐式展开.那是因为控件本身不是在初始化器中实例化的,而是在后面的阶段.

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. That's because the control itself is not instantiated in the initializer, but at a later stage.

这篇关于Swift 类中的错误:在 super.init 调用时未初始化属性 - 如何初始化需要在其初始化参数中使用 self 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Swift 类中的错误:在 super.init 调用时未初始化属性 - 如何初始化需要在其初始化参数中使用 self 的属性

基础教程推荐