【问题标题】:What is difference between static and class keyword in swift? [duplicate]swift中的static和class关键字有什么区别? [复制]
【发布时间】:2020-05-13 19:16:42
【问题描述】:

我正在寻找 swift 中 static 和 class 关键字之间的区别。我需要在哪里使用静态与类。

例如;

class MyClass {

    static func firstMethod() {}

    class func secondMethod() {}
}

 func doSomething() {
     myClass.firstMethod()
     myClass.secondMethod()
 }

所以我们可以这样使用;

MyClass.secondMethod()

MyClass.firstMethod()

那么我应该在哪里使用静态或类?

【问题讨论】:

标签: ios swift class static


【解决方案1】:

一般来说,使用static 关键字就足够了,但是Apple 提供了描述,如果你使用struct - 使用static,if class - 使用class

static 与class final 相同

但我建议阅读下面的文章,我希望阅读后一切都会变得更干净 https://borgs.cybrilla.com/tils/global-vs-static-function-swift/

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2011-10-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    相关资源
    最近更新 更多