Re: self = [super init], nil?
Re: self = [super init], nil?
- Subject: Re: self = [super init], nil?
- From: Kyle Sluder <email@hidden>
- Date: Fri, 7 May 2010 21:11:18 -0700
On May 7, 2010, at 11:34 AM, "Patrick M. Rutkowski"
<email@hidden> wrote:
Will NSObject's init method ever really return nil?
Realistically, no. If it does, there's nothing you can do about it,
and you might as well abort. You've probably run out of memory, and I
don't believe there's any guarantee that the runtime will be in a
consistent state if it can't allocate any objects. I could be wrong
about that, but I still wouldn't recommend it.
E.g. if I sub-class NSObject, then is it worth checking for nil after
doing self = [super init]?
I say yes for the sake of muscle memory. I like to combine them into
one statement: if(!(self = [super init])) return nil;
-init will be called a maximum of once per object. It's worth the
branch to check for this case, if for no other reason than to avoid
crashing on NULL pointer dereference. You can even use the GCC branch
prediction intrinsics to minimize the impact of the branch.
My $0.02 (worth more in Europe every day!)
--Kyle Sluder
_______________________________________________
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