• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Properly writing an Init method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Properly writing an Init method


  • Subject: Re: Properly writing an Init method
  • From: Ondra Cada <email@hidden>
  • Date: Thu, 18 Jul 2002 14:44:41 +0200

On Thursday, July 18, 2002, at 06:35 , Julian Barkway wrote:

The above discussion is very interesting but there's also another point in the example code. I have often wondered about the merits of 'self = [super init]'. Surely this means that 'self' effectively becomes typed to the superclass rather than the class itself?

'Course not. This is dynamic type system, not a static one.

Fair point. So what are the benefits of assigning to self in this way, rather than using, say,

if (![super init])
return nil;
// Init stuff...
return self;

I'm only asking because I recently had a problem which was caused by an object being typed to it's own superclass, rather than the class it should have been typed to, and it struck me this could have been caused by my use of 'self = [super init]'...

First, the self=[super init] convention allows for switching to quite a different object. The simplest (agreeably somewhat artifical) case might be

@implementation Strange:NSObject
-init {
if (!(self=[super init])) return nil; // unimportant here
if (/* whatever */) {
[self autorelease];
return [[[self class] alloc] init];
}
return self;
}
...
@end

(Depending on the "whatever" condition there might be a danger of infinite loop; let's just presume the condition is smart enough to prevent that, for it is not important for the thing we are speaking of here).

Now, to the "typing". Object can't be "typed" in ObjC (unless you dive pretty low to change isa -- if you don't know what I am talking about, never mind, you don't need that). The only thing which might happen sometimes is that the object you are using happens to be an object of different class that you presumed. That would mean a bug in sources, like

NSString *aString=[NSData data];

or somewhat more obscure, but just as wrong

-init {
...
if (...) ... return [[Strange alloc] init];
...
}

On the other hand, with the proper code shown above, there's no danger.
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Properly writing an Init method (From: Julian Barkway <email@hidden>)

  • Prev by Date: Re: static class variable
  • Next by Date: Re: [Seriously OT]Mac.com 60 days and counting
  • Previous by thread: Re: Properly writing an Init method
  • Next by thread: Re: Properly writing an Init method
  • Index(es):
    • Date
    • Thread