Shared Store and allItems array


#1

I’m getting confused with the sharedStore method. When you call it, wouldn’t it return a variable pointing to nil as you point it to nil at the start of the method? Or does it ignore it? Also with the allItems array instance variable couldn’t you just declare it a readOnly property instead of implementing a getter method?


#2

static variables are only set once! The next time sharedStore method is called, imagine the following…

the code is read up to this point… [color=#FF0040]static BNRItemStore *sharedStore |[/color] at this stage, runtime is saying, “HEY wait a minute here… this variable already exists!” so it never finishes reading the line and sharedStore is NOT set to nil. As a result, the if (!sharedStore) statement gets skipped and the previously created sharedStore gets returned.

This is the way I like to think about it.


#3

[quote=“sandmanjr”]static variables are only set once! The next time sharedStore method is called, imagine the following…

the code is read up to this point… [color=#FF0040]static BNRItemStore *sharedStore |[/color] at this stage, runtime is saying, “HEY wait a minute here… this variable already exists!” so it never finishes reading the line and sharedStore is NOT set to nil. As a result, the if (!sharedStore) statement gets skipped and the previously created sharedStore gets returned.

This is the way I like to think about it.[/quote]

Thanks for the explanation, now I also get it! :slight_smile: