small medium large xlarge

Generic-user-small
02 Nov 2010, 20:10
Ola Lindgård (2 posts)

I have a question regarding the re-written ‘init’-methods:

The ‘initWithNumber’ is declared in the @interface-file, and in the @implementation file. Why is the standard ‘init’-method not declared in the @interface file? I understand that ‘initWithNumber’ calls ‘super’ to initialize properly…but still. I tried to declare the standard ‘init’ method in the WonderfulNumber´s interface file anyway, and the compiler did not seem to complain.

My question: Why is there no declaration of ‘init’ in the WonderfulNumber header?

Fantastic book by the way, thanks. -Ola

Generic-user-small
05 Nov 2010, 19:52
Tim Isted (105 posts)

Hi Ola,

It’s absolutely fine to declare the basic @init@ method inside the @WonderfulNumber.h@ file, but there are several reasons why you don’t need/want to:

a) it’s already declared in @NSObject@’s interface file, so @init@ is automatically inherited by any class that is a subclass of @NSObject@ (i.e., most classes). By itself, this isn’t enough of a reason not to re-declare it in any subclass’ interface, but it’s an explanation of why you don’t need to.

b) @init@ is one of the most (possibly the most) commonly overridden method in any class file. Because of this, and because it’s so well known to create an object using @[[SomeClass alloc] init]@, it really isn’t necessary to include it in any subclass’ interface. If a class inherits from @NSObject@, which most of the classes you will encounter do, you can simply assume that any instances will happily receive an @init@ message.

See also page 40 of the printed book (1st Printing, anyway), Chapter 3, near the end of the section titled “Setting up our Class Description” for a brief explanation of this.

Generic-user-small
08 Nov 2010, 20:51
Ola Lindgård (2 posts)

Thank you for your response.

Love the book!

You must be logged in to comment