Access static variables within class in Swift

后端 未结 7 2157
独厮守ぢ
独厮守ぢ 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:36

    In a future Swift 3 version (yet to be released) you can use Self (yes, that's with a capital) to reference to the containing class. A proposal for this was accepted, but the feature is not implemented yet.

    For example:

    struct CustomStruct {          
     static func staticMethod() { ... } 
    
     func instanceMethod() {          
       Self.staticMethod() // in the body of the type          
     }          
    }
    

    Source: https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md

提交回复
热议问题