No need to thank me for my patience! Trust me, I’ve been in your shoes not too long ago and it is rough. I hate not understanding things and then having to carry on until it clicks. Try this explanation out though. When you see something like this:
know that “super” is merely a synonym for the class of the instance receiving the message. In our case, we sent a message to the BNRItemStore class. In addition to being a synonym, super also instructs our class (BNRItemStore in this case) which method implementation to select. We (BNRItemStore) were sent the message +sharedStore whose implementation allocates an instance of BNRItemStore class using our superclass’s +allocWithZone: implementation.
You are right in thinking that [AnyClass alloc] means allocate memory and return a pointer to an instance of AnyClass. It is no different with super. super is AnyClass. What is different is that you aren’t using AnyClass’s +alloc method. Rather you are using the superclass’s +alloc to allocate an instance of AnyClass.
[AnyClass super] alloc] is wrong. Intuitively, you may think that super returns the superclass of the AnyClass class but that’s not the case. The keyword super can only be used as the receiver of a message. As said above, super IS self. They are pointers to the same object. They differ in which method implementation is selected for the message received. A message with self as the receiver looks for a method in the receiver’s class with the same signature. A message with super as the receiver looks for a method in self’s superclass with the same signature. If one isn’t found in the immediate superclass, the search continues up the inheritance hierarchy.
Using BNRItem as an example, if we overrode the -isEqual: method (-isEqual: is a method in a category on NSObject) like so:
we will be comparing two BNRItem instances using the -isEqual: implementation of our superclass (NSObject). The receiver and argument (presumably) will BOTH be BNRItem instances. NSObject’s implementation only compares the pointer values, so this implementation is not adequate. It merely is to reiterate the distinct method selection of self and super.
The takeaway is that self and super ARE the same instance. The only difference is where the search for method selection begins, the class of the receiver (self) or the superclass of the receiver (super).
As an aside, this is no different when you override an -init method. You typically assign self (an implicit local variable that points to the receiver of the message) to the returned pointer of [super init]; By doing this:
self = [super init];
// initialization here
You are initializing the instance returned from +alloc but using your superclass’s designated initializer (in this case, plain init) subsequently followed by class specific initialization. Since we are overriding -init we need a way to initialize our inherited instance variables AND do so without a loop (This is why it is not [self init] We assign self to this returned instance and then continue our custom initialization.
Hope this helps a bit. If not, reply and we’ll get it straightened out.