views:

919

answers:

4

There are a couple of things about Objective-C that are confusing to me:

Firstly, in the objective-c guide, it is very clear that each class needs to call the init method of its subclass. It's a little bit unclear about whether or not a class that inherits directly from NSObject needs to call its init method. Is this the case? And if so, why is that?

Secondly, in the section about NSObject, there's this warning:

A class that doesn’t need to inherit any special behavior from another class should nevertheless be made a subclass of the NSObject class. Instances of the class must at least have the ability to behave like Objective-C objects at runtime. Inheriting this ability from the NSObject class is much simpler and much more reliable than reinventing it in a new class definition.

Does this mean that I need to specify that all objects inherit from NSObject explicitly? Or is this like Java/Python/C# where all classes are subtypes of NSObject? If not, is there any reason to make a root class other than NSObject?

+2  A: 

1) Any time an object is allocated in Objective-C its memory is zeroed out, and must be initialized by a call to init. Subclasses of NSObject may have their own specialized init routines, and at the beginning of such they should call their superclass' init routine something like so:

self = [super init];

The idea being that all init routines eventually trickle up to NSObject's init.

2) You need to be explicit about the inheritance:

@instance myClass : NSObject { /*...*/ } @end

There is no reason to have a root class other than NSObject -- a lot of Objective-C relies heavily on this class, so trying to circumvent it will result in you needlessly shooting yourself in the foot.

fbrereto
As has been noted in other posts, there is sometimes a reason to have another root class than NSObject. Cocoa includes the additional root class NSProxy, which demonstrates the advantages of having NSObject be an explicit rather than implicit inheritance. You are correct that it is very unlikely that a third party developer should create a new root class.
Rob Napier
+1 Thanks for keeping me straight.
fbrereto
A: 

You need to call [super init] because there is code behind initializing that you dont have to write because it is written for you in NSObjects init, such as probably actual memory allocation etc.

Daniel
Tidbit: Some Cocoa classes actually inherit from `NSProxy`, which is another root class in the Cocoa framework. Both `NSObject` and `NSProxy` implement the `NSObject` protocol.
mipadi
+2  A: 

Since it is possible to inherit from different root base classes, yes you must explicitly declare you inherit from NSObject when you make any new class (unless of course you are subclassing something else already, which itself in turn probably subclasses NSObject).

Almost never is there a need to make your own base class, nor would it be easy to do so.

Kendall Helmstetter Gelner
+1  A: 

Objective-C can have multiple root classes, so you need to be explicit about inheritance. IIRC NSProxy is another root class. You'll likely never want or need to create your own root class, but they do exist.

As for calling NSObject's init, it's part custom and part safety. NSObject's init may not do anything now, that's no guarantee that future behaviour won't change. Call init to be safe.

Terry Wilcox