Singleton init method - not setting defaultStore?


Why doesn’t the init method of PossessionStore set the defaultStore static variable if it does not yet exist? If defaultStore is nil, as it is if the singleton has not yet been created, then it will simply call NSObject’s init method, and it will never set the defaultStore. Therefore, if we call init again, it will create two instances of our singleton, since defaultStore will still be nil and the if block will not get executed. So shouldn’t it set the defaultStore variable to prevent multiple instances of the singleton?

Here’s the code as it is written in the book:

- (id)init
    if (defaultStore) {
        return defaultStore;

    self = [super init];
    return self;


The allocWithZone: and defaultStore methods, in conjunction with the init method, guarantee the unique instance of PossessionStore.

Try tracing the code from the following possibilities:

[[PossessionStore alloc] init];
[PossessionStore defaultStore]