I am following one example to learn about singletons, but I am little confused with the code, particularly with the usage of static variable. Following is the code: This is where code checks if this is the first time variable is initialized.
+ (instancetype)sharedStore { static BNRImageStore *sharedStore = nil; if (!sharedStore) { sharedStore = [[self alloc] initPrivate]; } return sharedStore; } If it is the first time initPrivate: method is called:
- (instancetype)initPrivate { self = [super init]; if (self) { _dictionary = [[NSMutableDictionary alloc] init]; } return self; } I have a problem with first part of code, with sharedStore: method. How this variable sharedStore can retain data, when every time, we call this method to get singleton, sharedStore is made to point to nil.
Code works fine, so definitely there is nothing wrong with it. Does this mean if variable is static static BNRImageStore *sharedStore = nil; will be ignored.
Thanks in advance and above code is taken from the book I am reading "IOS Programming: The BNR Guide".