• 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: !foo vs foo == nil
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: !foo vs foo == nil


  • Subject: Re: !foo vs foo == nil
  • From: "Michael Ash" <email@hidden>
  • Date: Wed, 20 Aug 2008 23:03:10 -0400

On Wed, Aug 20, 2008 at 7:56 PM, John C. Randolph <email@hidden> wrote:
>
> On Aug 20, 2008, at 4:15 PM, Torsten Curdt wrote:
>>
>> There was a common perception that NULL is not really the same as nil. But
>> seems like in the end it really is (void*)0.
>
> They differ in type, not in value.
>
> "NULL" is (void *) 0.
> "nil" is (id) 0.
> "Nil" is (Class) 0.

This is true conceptually but not as far as their actual definition.
NULL can be either 0 or (void *)0. Nil and nil don't have a formal
definition, as Objective-C lacks a formal specification, but on my
machine they are both defined to be either 0 in 32-bit or (0L) in
64-bit. But for maximal clarity you should use them as you specify
above.

> Personally, I prefer "if (!foo)" over "if (foo == nil)", because the latter
> has the hazard of a typo that compiles.  You can lose a fair bit of time
> staring at "if (foo = nil)" before you spot the mistake.

This hazard goes away if you turn on the appropriate warnings. I
compile all of my code with "-W -Wall -Wno-unused-parameter", and it
has caught much more than just this error over the years.

Mike
_______________________________________________

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: !foo vs foo == nil
      • From: Thomas Engelmeier <email@hidden>
    • Cheap quality improvements (was Re: !foo vs foo == nil)
      • From: Neil Brewitt <email@hidden>
References: 
 >!foo vs foo == nil (From: Torsten Curdt <email@hidden>)
 >Re: !foo vs foo == nil (From: Filip van der Meeren <email@hidden>)
 >Re: !foo vs foo == nil (From: Torsten Curdt <email@hidden>)
 >Re: !foo vs foo == nil (From: "John C. Randolph" <email@hidden>)

  • Prev by Date: Accessing properties of a generic class
  • Next by Date: Re: !foo vs foo == nil
  • Previous by thread: Re: !foo vs foo == nil
  • Next by thread: Cheap quality improvements (was Re: !foo vs foo == nil)
  • Index(es):
    • Date
    • Thread