初始化静态实例时如何运行编码?

How to run code when a static instance is initialized?

我正在使用这种模式:

open class CoreDataHub {

public static let sharedInstance = CoreDataHub()
  open class func getThing() -> String {
    return "hello world"
  }
  init() {
    sharedInstance.getThing()
  }

}

但是我收到以下错误:

Static member 'getThing' cannot be used on instance of type 'CoreDataHub'

我的目标是 运行 一些代码...即在初始化 sharedInstance 时调用 getThing()。有任何想法吗?谢谢

您正在获得

Static member 'getThing' cannot be used on instance of type 'CoreDataHub'

因为,您使用 sharedInstance 对象调用 getThing() 静态方法。

使用CoreDataHub.getThing()代替sharedInstance.getThing()

public static let sharedInstance = CoreDataHub()
init() {
    let value = CoreDataHub.getThing()
    
}