• 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 10:13:25 -0300

Patrick,


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?


Cheers,
Flavio


On 08/05/2010, at 01:23, Patrick M. Rutkowski wrote:

> Will NSObject's init method ever really return nil?
>
> E.g. if I sub-class NSObject, then is it worth checking for nil after
> doing self = [super init]?
>
> I know there are many classes in UIKit and Cocoa which most definitely
> can return nil from their -init's, as an indication of failure. But
> will this ever be the case with NSObject?
>
> -Patrick

_______________________________________________

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: Joanna Carter <email@hidden>
    • Re: self = [super init], nil?
      • From: Flavio Donadio <email@hidden>
  • Prev by Date: Re: NSBrowser dragging
  • 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