• 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: self = [super init], nil?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: self = [super init], nil?


  • Subject: Re: self = [super init], nil?
  • From: Flavio Donadio <email@hidden>
  • Date: Sat, 8 May 2010 14:00:51 -0300

Joanna,


You're right.

On 08/05/2010, at 11:06, Joanna Carter wrote:

> 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;
> }


I made lots of mistakes in my example code. I meant to write exactly what you wrote above, except for:

if (self = [super init])

which I would replace with:

if ([super init] != nil)

Let's say I create a subclass of NSObject (let's call it "MyOddObject") and override its -init method:

- (id)init
{
	self = [super init];
	// blah blah blah ...
	return self; // return type will be NSObject!!
}

And then I instantiate this class afterwards:

MyOddObject* myOddInstance = [[MyOddObject alloc] init];

I should get an instance of MyOddObject, but I am getting a NSObject instead. All right, I think the above line also casts the return value to MyOddObject. Am I right?

My question is: apart from the optimization advantages cited by James Bucanek, why would one set self's value to the return value of [super init]? Wouldn't it be confusing?


Cheers,
Flavio_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: self = [super init], nil?
      • From: James Bucanek <email@hidden>
    • Re: self = [super init], nil?
      • From: Kyle Sluder <email@hidden>
References: 
 >Re: self = [super init], nil? (From: Flavio Donadio <email@hidden>)
 >Re: self = [super init], nil? (From: Joanna Carter <email@hidden>)

  • Prev by Date: Re: self = [super init], nil?
  • Next by Date: Re: self = [super init], nil?
  • Previous by thread: Re: self = [super init], nil?
  • Next by thread: Re: self = [super init], nil?
  • Index(es):
    • Date
    • Thread