Singleton objective c clarification

前端 未结 5 917
青春惊慌失措
青春惊慌失措 2020-12-12 02:18

as I continue my studies the book implemented a singleton. I understood the reason why use it but I just wanted some clarification regarding the code.

+ (BN         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 02:47

    It's important to understand that the static keyword has two effects. One is that it makes that variable exist before the method is called, and persist after it returns, so that it will be available for the next call. The other effect is more subtle -- the "assignment" that initializes the static variable is executed when the code is loaded, not when the method is called. So it does not get reinitialized on every call.

    But since the variable exists "outside" of the method, it's name should be unique -- don't use the same name in another singleton in this class or another one.

提交回复
热议问题