In Objective C, Usual implementation of singleton design pattern contains “static id theInstance = nil” in a method, why not outside?

后端 未结 1 1946
野性不改
野性不改 2021-01-16 10:12

When i was going through Singleton design pattern in Objective C, I found lot of people using the below code to create it.

@interface Base : NSObject {} 

+(         


        
相关标签:
1条回答
  • 2021-01-16 10:55

    static variables only get initialized once, regardless of if they're at global or local scope. In this case, you don't even need the nil - static storage class variables are zero-initialized by default. This declaration:

      static id theInstance;
    

    is enough to be the same as what you have there.

    0 讨论(0)
提交回复
热议问题