When to use static constant and variable in Swift?

后端 未结 4 1663
刺人心
刺人心 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条回答
  •  -上瘾入骨i
    2020-12-07 13:50

    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)
      

提交回复
热议问题