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

查看:23
本文介绍了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.The Swift Programming Language".电子书.https://itunes.apple.com/us/书/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 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆