When to use static constant and variable in Swift?

When you define a static var/let into a class (or struct), that information will be shared among all the instances (or values).

Sharing information

class Animal {
    static var nums = 0

    init() {
        Animal.nums += 1
    }
}

let dog = Animal()
Animal.nums // 1
let cat = Animal()
Animal.nums // 2

As you can see here, I created 2 separate instances of Animal but both do share the same static variable nums.

Singleton

Often a static constant is used to adopt the Singleton pattern. In this case we want no more than 1 instance of a class to be allocated. To do that we save the reference to the shared instance inside a constant and we do hide the initializer.

class Singleton {
    static let sharedInstance = Singleton()

    private init() { }

    func doSomething() { }
}

Now when we need the Singleton instance we write

Singleton.sharedInstance.doSomething()
Singleton.sharedInstance.doSomething()
Singleton.sharedInstance.doSomething()

This approach does allow us to use always the same instance, even in different points of the app.


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 and static variable rather than constant and variable. Can someone explain? When you define a static var/let into a class (or struct), that value will be shared among all the instances (or values).

static variables/class are variables can be accessed without need of creation of any instance/object.

class Human {
    static let numberOfEyes = 2 //human have only 2 eyes
    static var eyeDefect = false //whether human have side-effect or not. he can have defect later so its variable

    //other variables and functions
}

//you can access numberOfEyes like below no object of Human is created
print(Human.numberOfEyes)
print(Human.eyeDefect)

//Object of Human
let john = Human()

I think you know difference between constant and variable. In short, constant is that whose value never changes; numberOfEyes in above example and variable is that whose value changes; eyeDefect in above example.

static constant or variables are placed in memory(RAM) separate then the Objects. i.e. numberOfEyes have different memory space allocated than John object, its not inside John.

now, when to use static constants/variables:

  1. When you use singleton design pattern: static let sharedInstance = APIManager()

    class APIManager(){
        static let sharedInstance = APIManager()
        //Your other variables/functions here below
    }
    //Use it as to get singleton instance of APIManager from anywhere in your application
    let instanceOfAPIManager = APIManager.sharedInstance
    
  2. When you need value of anything that is globally the same without need to make instance of the class under which it is defined like numberOfEyes in human class.

  3. Use of static variables/constants are not much recommended because of memory issues because once it's instantiated/assigned, it remains in memory until your application gets removed from the memory. I have found till now the best place to use static variables/constants is only while making singleton pattern and sometimes pointers for other normal variables and constants don't use static because: memory issue, it will be difficult to run unit testing in your code with static variables/constants. Not recommended to use as like in Human class also. instead use them as just constant or variables and access them by making instance.

    class Human {
     let numberOfEyes = 2 //human have only 2 eyes
     var eyeDefect = false //whether human have side-effect or not. he can have defect later so its variable
    
       //other variables and functions
     }
    
    //you can access numberOfEyes like below if you need just those values.
    print(Human().numberOfEyes)
    print(Human().eyeDefect)