Singleton objective c clarification

前端 未结 5 915
青春惊慌失措
青春惊慌失措 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:32

    For function-static variables the line

    static BNRItemStore *defaultStore = nil;
    

    is not an assignment. Rather, it is static initialization, which happens only once - the first time the code goes through your function. In subsequent invocations the value will not be nil, because you assign a non-nil value to it.

    Your implementation is safe in single-threaded environments. For concurrent environments you would need to add some form of synchronization.

提交回复
热议问题