0

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".

2 Answers 2

0

the issue is that the sharedStore is declared static and NOT a local variable. A static variable is never destroyed. Static variables do not live on the stack and thus setting it to nil doesn't destroy it. So, thus in the case you've shown here the setting it to nil doesn't do anything and it doesn't enter the conditional which will create it. It's a bit counter-intuitive but does work this way.

A simple way of showing this static nature is provided in the code here: Why should you check a static variable for nil if it was initialized to nil on the previous line? namely:

void plugh(void) { static int xyzzy = 0; printf (" %d", xyzzy); // or Obj-C equivalent. xyzzy++; } 

and calling it 100 times will produce:

0 1 2 3 4 ... 

There's another way to vend a singleton using dispatch_once. An example is provided here with the sharedWeatherHTTPClient http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for answer. Does that mean BNRImageStore *sharedStore = someOtherBNRImageStoreVariable; won't change anything either.
hmm... not 100% about what you're asking in comment. Usually, things like shared stores have like a backing object like this in the case of the NSMutableDictionary and they provide methods that allow you to access or add to it. So normally it's like [[BNRImageStore sharedInstance] addObect: obj forKey:key]; or something like that.
What my question is when we make sharedStore pointer to point to nil, doesn't that mean we will lose the memory address of the data it was pointing to before.
not in that method. I am pretty sure if you set it to nil in another method, then, yes, it will affect sharedStore. I'm more familiar with it as a pattern. see nshipster.com/c-storage-classes and look at the static singleton section.
Static variables have scope, and in this case the scope IS local. The value is remembered across invocations. Sandeep, sharedStore is scoped to be only available within that method. It's set to nil when it's declared and initialized. If you were to set it to nil on the line after initPrivate, yes, you would lose the address you just set on the initPrivate line.
|
0

Static variables are initialized only once thus the code static BNRImageStore *sharedStore = nil; will only runs once no matter how many times you call the method sharedStore

You might have studied the storage classes in c and there are four

  1. auto
  2. extern
  3. static
  4. register

And for static varibales the initialization is performed once

NOTE: There is a difference between initialization and assignment.

Initialization : When you assign the value at the time of definition. e.g.

int a = 5; //this is initialization 

Assignment : When you assign a value after definition. e.g.

int a = 0; //initialization a = 6; //assignment int b; b = 10 //assignment 

You can say that initialization is special case of assignment

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.