[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re[2]: Problem with +numberWithBool: ?
From: |
Manuel Guesdon |
Subject: |
Re[2]: Problem with +numberWithBool: ? |
Date: |
Thu, 29 Jan 2004 16:49:23 +0100 (CET) |
On Thu, 29 Jan 2004 13:12:30 +0000 Richard Frith-Macdonald <address@hidden>
wrote:
>|
>| 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
But it's not NO either :-)
>| Booleans in objc can be YES or NO ...
No:
typedef unsigned char BOOL;
#define YES (BOOL)1
#define NO (BOOL)0
So a BOOL can have a value between 0 and 255.
>| 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 agree for NO comparaison but not on YES comparaison as I can imagine a lot of
situations where programmer can make the
'mistake'.
For exemple:
o [NSNumber numberWithBool:strcmp(string1,string2)]
o [NSNumber numberWithBoo:a!=b]
For this one, could we be sure that (BOOL)(a!=b) will always
return only 0 or 1 ? On all processors ?
>| > 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
Thinking of Objective-C as a langage over/derived from C, I can't agree: I
think that for many (most of ?) developpers 0
is false/NO and true/YES is !(false/NO).
Manuel
--
______________________________________________________________________
Manuel Guesdon - OXYMIUM <address@hidden>
14 rue Jean-Baptiste Clement - 93200 Saint-Denis - France
Tel: +33 1 4940 0999 - Fax: +33 1 4940 0998
- Problem with +numberWithBool: ?, Manuel Guesdon, 2004/01/29
- Re: Problem with +numberWithBool: ?, Richard Frith-Macdonald, 2004/01/29
- Re: Problem with +numberWithBool: ?, David Ayers, 2004/01/29
- Re: Problem with +numberWithBool: ?, Alexander Malmberg, 2004/01/29
- Re[2]: Problem with +numberWithBool: ?,
Manuel Guesdon <=
- Re: Problem with +numberWithBool: ?, Helge Hess, 2004/01/30
- [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?), Alexander Malmberg, 2004/01/30
- Re: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?), David Ayers, 2004/01/30
- Re: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?), Nicola Pero, 2004/01/30
- Re[2]: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?), Manuel Guesdon, 2004/01/30
- Re[2]: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?), Nicola Pero, 2004/01/30
- Re[3]: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?), Manuel Guesdon, 2004/01/30