Access static variables within class in Swift

后端 未结 7 2203
独厮守ぢ
独厮守ぢ 2020-12-08 09:24

Is ClassName.staticVaribale the only way to access static variable within the class? I want something like self, but for class. Like class.st

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 09:51

    You could work around this by defining a self referencing typealias.

    class MyClassWithALongName {
      typealias CLASS = MyClassWithALongName
    
      static let staticFoo = "foo"
    
      func someInstanceMethod() -> String {
        return CLASS.staticFoo
      }
    }
    

    Though the style-guide gods may not approve.

提交回复
热议问题