> I don't really know if I am right, because of lack of experience. Please, 
> correct me if I'm wrong, but... Why would anyone write code like that?
> 
> I mean, if one subclasses NSObject and doesn't override +init, it will return 
> self -- or, maybe, nil -- anyways.
> 
> So, I would assume that [super init] would only be called inside an +init or 
> +initWithWhatever: method, right? Like this:
> 
> + (id*)init
> {
>       [super init];
>       // initialize some other instance vars, etc. ...
>       [return self];
> }
> 
> So, in case a class' +init method can return nil and you want to subclass it, 
> the code should be more like:
> 
> + (id*)init
> {
>       if ([super init] != nil)
>       {
>               ...
>               [return self];
>       }
>       else
>       {
>               return nil; // or return something else, throw an exception, 
> whatever...
>       }
> }
> 
> I am just curious, because I think it's strange to write "self = [super 
> init]" anywhere other than in an overriden +init. Am I right?

If you declare +init, you are writing a static method, not an instance method. 
The static initialiser is +initialize and you would not call [super 
initialize], as the superclass's initialiser is called before your class's.

-init is an instance method and it is that method that you should be 
overriding. Either that, or calling [super init] from your own instance 
initialiser.

Your first example calls [super init] but doesn't assign it to self. Which is a 
good thing because self, in the case of a static initialiser, returns the 
Class, not the instance.

BTW, you should not declare id* as the return type, it should be simply id.

- (id) init
{
  if (self = [super init])
  {
    // rest of initialisation
  }

  return self;
}

Joanna

--
Joanna Carter
Carter Consulting

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to