gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with +numberWithBool: ?


From: Richard Frith-Macdonald
Subject: Re: Problem with +numberWithBool: ?
Date: Thu, 29 Jan 2004 13:12:30 +0000


On 29 Jan 2004, at 12:24, Manuel Guesdon wrote:

Hi,

+numberWithBool: is defined as:

        + (NSNumber*) numberWithBool: (BOOL)value
        {
          if (value == YES)
            {
              return boolY;
            }
          else
            {
              return boolN;
            }
        }

So as far as I understand [NSNumber numberWIthBool:2] will return boolN which is wrong as 2 is TRUE, isn't it ? (Or may
I need some sleep ? :-)

I agree it'll return boolN ... but 2 is not YES
Booleans in objc can be YES or NO ... this is not the same thing as true/false in C/ObjC conditionals where 0 is false and anything non-zero is true.
So ...

if (x)...
is not the same as
if (x == YES)
where x is of type BOOL

which is why I prefer to write code which explicitly compares booleans values with YES or NO.

I suggest:
        + (NSNumber*) numberWithBool: (BOOL)value
        {
          if (value == NO) // NO is always 0 but YES is a value != 0
            {
              return boolN;
            }
          else
            {
              return boolY;
            }
        }


I think it might be better to raise an exception if value is not YES or NO





reply via email to

[Prev in Thread] Current Thread [Next in Thread]