I am following the Big Nerd Ranch book on iOS programming.
There is a sample of a static class:
#import <Foundation/Foundation.h>
@interface BNRItemStore : NSObject
+ (BNRItemStore *) sharedStore;
@end
I have a problem undrstanding the bit below with question mark in the comments.
If I try to alloc this class, the overriden method will bring me to sharedStore
, which in turn sets the static pointer sharedStore
to nil. The conditional after will hit the first time because the pointer doesn't exist.
The idea is that the second time I am in the same place, it would not alloc a new instance and get the existing instance instead. However with static BNRItemStore *sharedStore = nil;
I am setting the pointer to nil and destroy it, isn't it? Hence every time I am creating unintentionally a new instance, no?
#import "BNRItemStore.h"
@implementation BNRItemStore
+ (BNRItemStore*) sharedStore
{
static BNRItemStore *sharedStore = nil; // ???
if (!sharedStore) {
sharedStore = [[super allocWithZone:nil] init];
}
return sharedStore;
}
+(id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
@end