【问题标题】:How to get class of instance and call class function in Swift 4?如何在 Swift 4 中获取实例类并调用类函数?
【发布时间】:2018-03-28 07:36:45
【问题描述】:

在 Objective-C 中,获取实例的类并在其上调用类方法很简单。你如何在 Swift 4 中做到这一点?

例如

MyObject * obj = [[MyObject alloc] init];
[[obj class] someClassMethod];

【问题讨论】:

标签: swift


【解决方案1】:

您可以使用type(of)方法获取一个对象的类,并使用它来调用它的类方法。

class MyObject {
  static func someClassMethod() {

  }
}
let obj = MyObject()
type(of: obj).someClassMethod()

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2020-05-31
    • 2012-06-04
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    相关资源
    最近更新 更多