bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSUnarchiver jumps over its -init


From: Wim Oudshoorn
Subject: Re: NSUnarchiver jumps over its -init
Date: Mon, 12 Apr 2004 09:36:02 +0200
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.3.50 (darwin)

Philip Mötteli <Philip.Moetteli@tele2.ch> writes:

> Hi
>
>
> I made a subclass of NSUnarchiver. In my subclass's -init method, I
> initialize some instance variables. Unfortunately, my -init method
> gets never calld, because of
>
> NSUnarchiver.m:415:
>
> - (id) initForReadingWithData: (NSData*)anObject
> {
>
>       self = [super init];
>
> }

This is correct.  You should overwrite the designated initializer
of a class.  For NSUnarchiver the designated initializer is:

- initForReadingWithData: (NSData*) anObject

> It would get called, if this line would be replaced with:
>
>       self = [self init];
>
> Apart from the fact, that init should get called by convention,

No this is against the conventions, because as far as I understand
the conventions are as follows:


Convention
----------

a) Every class has initializers whose method name start
   with - init....

b) Every class has a "designated" initializer.
   The designated initializer is normally the initializer
   with most parameters.  (Is this the most generic or the
   most specialized?  I am always confused about that).

c) The generic initializer are supposed to call
   the designated initializer.  

d) The designated initializer is supposed to call
   some init method of the super class.


That is, if your class has two initializers:

- init
- initWithParameter: ...

than the initWithParameter:  would be the designated initializer, 
and the init method would be something like:

- init
{ 
  // calculate default parameter:
  ....
  self = [self initWithParameter: ...]
  ....
  return self;
}


The designated initializer would be something like:

- initWithParameter: ...
{
   ....
   self = [super init...]; // init... is the designated initializer 
                           // of the super class. 
                           
   ....
   return self;
}



Rationale for this setup
------------------------

* If a subclass wants to alter the behavior of the super class
  it has only to override the designated initializer.
  Not all the initializer of all the super classes.

  This is because all the inits eventually end up in the designated
  initializer.

  Not only reduces this code in the subclass, but it also
  means that if in the super classes a new init method is 
  added the subclass will still work.


* The reason for calling in the designated initializer 
  [super init...]
  is to avoid the case that if some subclass of your
  class by accident overwrites the init method you 
  call you end up with an infinite loop.



Oh, BTW, this is just how I interpret the convention, and
it is along time ago I thought about this.  So I might be
wrong and corrections are welcome.
And as always, sometimes there are good reasons to deviate from
a  convention.

Hope this answers your question,

Wim Oudshoorn.




     





reply via email to

[Prev in Thread] Current Thread [Next in Thread]