Challenge: CarArrayController vs NSArrayController


#1

Hi Adam,

Is there any particular reason why in the solution file available from GIT for this chapter, the outlet to the Array Controller is created with a type of NSArrayController instead of CarArrayController?

It was my understanding that we had subclassed NSArrayController as CarArrayController to add the default date method, then changed the Array Controller in our XIB to CarArrayController.

In my own example I added @class CarArrayController; before my implementation, created the outlet with a type of CarArrayController, added an #import “CarArrayController.h” before my implementation (otherwise I got errors about forward declarations). Is this also correct?

This is the first chapter that I really struggled with, so I apologize in advance if this is a terribly simple question. My brain is rather fried trying to understand all the “magic” behind the scenes :slight_smile:

Thanks in advance!


#2

The outlet (instance variable) type is NSArrayController because, from the code, we don’t really care that it’s actually a CarArrayController (it could be a CarArrayController pointer as well). Why don’t we care from the code? Because the instance is created in the XIB, and we aren’t adding any new methods – just overriding an existing one. The type of the instance variable here doesn’t really matter, due to the dynamic nature of Objective-C (we can send any message to any object; it may not respond to that message, however). So the type of the pointer is really important for allowing the compiler to help us make sure we’re sending it the right messages.

Put another way, it’s fine to do something like this:

in particular because CarArrayController subclasses NSArrayController. Does that help? This touches on some more complicated issues so my answer is a bit more detailed than you perhaps need.

Adam


#3

Fantastic answer, you made it very clear, thanks!