Singleton objective c clarification

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

    Apple recommends something like the following

    + (BNRItemStore *)defaultStore
    {
        static BNRItemStore *defaultStore = nil;
        static dispatch_once_t done;
        dispatch_once(&done,
                      ^{ defaultStore = [BNRItemStore alloc]init];});
        return defaultStore;
    }
    

    The above code assumes ARC - If not using ARC you would have to define do nothing retain, release, autorelease, and dealloc methods.

提交回复
热议问题