• 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
[Obj-C] if (self) vs. if (self != nil)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Obj-C] if (self) vs. if (self != nil)


  • Subject: [Obj-C] if (self) vs. if (self != nil)
  • From: Oleg Krupnov <email@hidden>
  • Date: Fri, 24 Feb 2012 16:50:06 +0200

An interesting question. The following samples are equivalent in terms
of compiled code, but which one is more correct from the language's
point of view?

self = [super init];
if (self)
{
}
return self;

self = [super init];
if (self != nil)
{
}
return self;

The Xcode samples promote the first variant, but I'm wondering if the
second one is more correct?

The "nil" is defined as follows (jumped to definition)

#define nil NULL
#define NULL ((void*)0)

So basically, nil is of type "void*", so the expression "self != nil"
compares two pointers and the result is "boolean", which is perfect
for testing in the "if" statement. But the "self" alone is of type
"pointer" and so when it is tested by the "if" statement, it's
implicitly cast to the type "boolean".

I also heard that generally speaking NULL is not necessarily always
equal to 0 on all architectures.

Thoughts?
_______________________________________________

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: [Obj-C] if (self) vs. if (self != nil)
      • From: William Squires <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Greg Parker <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Nick Zitzmann <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Kyle Sluder <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Kyle Sluder <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: David Rowland <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Wade Tregaskis <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Scott Ribe <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Thomas Davie <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Keary Suska <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Fritz Anderson <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: David Duncan <email@hidden>
    • Re: [Obj-C] if (self) vs. if (self != nil)
      • From: Howard Moon <email@hidden>
  • Prev by Date: Re: ImageCapture + ImageKit problem...
  • Next by Date: Re: What to use instead of operatingSystemVersionString
  • Previous by thread: Re: What to use instead of operatingSystemVersionString
  • Next by thread: Re: [Obj-C] if (self) vs. if (self != nil)
  • Index(es):
    • Date
    • Thread