简体   繁体   中英

BOOL vs. bool on iOS7

In iOS we came across a problem with BOOL vs. bool.

I know that one is a bitfield and the other is an integer.

However the following code behaves differently on iOS7 and iOS8:

self.nativationItem.rightBarButton = _editButton; //where editButton is a UIBarButtonItem.

_editButton.enabled = _some_NSArray.count;

since count is defined as an NSUInteger , we expect it to be 0 (false) or true for any other value that is > 0.

However, on iOS7 when _some_NSArray.count > 0, the editButton is disabled but on iOS8 the editButton is enabled! The exact same code.

Another thing is that if we cast the _some_NSArray.count to a "bool" (not with capital letters), it works on both but if we cast it to (BOOL) it works on 8 but not 7.

Any ideas?

enabled is BOOL . count is NSUInteger . The proper code would be:

_editButton.enabled = _some_NSArray.count > 0;

BOOL should only be assigned YES or NO values (or the equivalent result of a conditional expression).

BOOL is

typedef signed char BOOL;

defined in objc.h

bool is

#define bool _Bool

defined macro from stdboo.h which is same meaning bool in C language

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM