Have any Question?

You can ask below or enter what you are looking for!

Static properties in Swift

What is Static property

We all know Instance Properties that belong to an instance of type and every time you create an instance of that type it has its own property value.

But what if you want to create a global property that all instances of the same type share?

This is what we call Static or Type property because it belongs to the Type itself not an instance of it.

There will be always one copy of Type property that all instance can use it.

A Static/Type property can be Stored or Computed just like instance properties.

A stored type property must have a default value.

When to use Static or Type Property

Use Static property when you don’t need to have different value of property for every instance.

For example base url or maximum level of sound volume.

How to use Static or Type Property

class DemoClass {
    //Stored Type Property Variable
    static var storedVariableTypeProperty = "Some Value"
    //Stored Type Property Constant
    static let storedConstantTypeProperty = "Some Other Value"
    //Computed Type Property
    static var computedTypeProperty : Int {
        return 5
    //If you want a computed type property to be overridable by subclasses
    //use class keyword instead of static
    class var overridableComputedTypeProperty : Int {
        return 10


Leave a Reply

Your email address will not be published. Required fields are marked *