sharedStore relation to allItems


#1

Forgive me if this is a weak question, but I just don’t understand the relationship between sharedStore and allItems. I don’t see how setting sharedStore as static affects allItems. In the same line, why do we have to first send he message sharedStore before accessing the items in allItems?

Thanks.


#2

Well it looks like this was posted a while ago, but I’ll do my best to answer for any future readers…

I think there are a couple important things to understand here. One important thing is that allItems is an instance variable and is also accessed by an (-)instance method of the same name. An instance is really just a place in memory which holds a certain data type, and can be pointed to by a (*)pointer. Another thing is that, sharedStore is a (+)class method that returns a static variable, which is a pointer. A pointer to a space in memory given to it by the super class method allocWithZone:.

Consequently, since this is a static variable that can only be accessed within sharedStore and that contains a pointer, and since we have overwritten our class’s allocWithZone: method to call sharedStore, no allocation of memory can be made without calling sharedStore, which then will always return the same pointer to a single place in memory and that doesn’t get thrown out since it is static. Now because sharedStore is a class method that returns the same pointer to the same instance of the BNRItemStore class, this means that we can have no other instance of the BNRItemStore class (or rather, any pointer to any other space in memory where an instance of BNRItemStore exists). Therefore, there is only one instance of BNRItemStore that can call the instance method allItems and there will only be one allItems array containing the BNRItems that we have created.

So making the sharedStore variable static allows the pointer to this single place in memory to not be destroyed since it doesn’t live on the stack AND it also allows us to limit access to this variable by putting it within the sharedStore method since it has the same scope as a local variable, (check right below the “Creating the BNRItemStore” section in Ch. 9). And we must call the sharedStore method before we call the allItems instance method, because we must first have an instance of BNRItemStore that can call the allItems method, (which might also be called an accessor or convenience method I believe). And as I’ve said this can only be accomplished through sharedStore (or alloc which will in turn just call sharedStore).

I hope that helps someone and that I’ve explained it all rightly. Sorry if that is all too long, I just wanted to be thorough for those whose brains feel especially fatigued as mine often does.

Good luck.


#3

That’s the key point: a singleton can only ever have one instance, and only that one instance (here, sharedStore) can access the class’s methods and instance variables.


#4

Thanks, jgelling and starryVerre. I guess the thing I’m still a bit confused about is what makes it illegal or impossible to simply ask for allItems without the sharedStore method? In all other cases, since the header file was imported the implementation had direct access to the imported classes methods. Here though, we have to access allItems method via sharedStore and I don’t understand what is preventing …

Never mind, it just hit me, I think.


#5

Here is a related question which has me confused. On page 195 of the book, why is “allItems” declared as a pure instance variable rather than by using a property?


#6

Well allItems is supposed to be a private instance variable only accessible by BNRItemStore.

Declaring it as a property in the header file would make it publicly accessible. That’s the worst thing you could do.

It really should be declared in the implementation section (the .m) to be truly private to the class.


#7

Thanks, jgelling. I think that’s what was confusing me. I’ve always understand that those declared properties are publicly accessible when the header file is imported.