When to use static constant and variable in Swift?

后端 未结 4 1665
刺人心
刺人心 2020-12-07 13:16

There are some posts for how to write code for static constant and static variable in Swift. But it is not clear when to use static constant<

4条回答
  •  执笔经年
    2020-12-07 13:53

    Static constants and variables do belong to the class itself, not to a particular instance. A class can also have static methods that can be called without creating an instance of a class.

    So when you have a class MyClass with a static var x, you can also access it through MyClass.x directly. x will be shared among all instances of a class

提交回复
热议问题