OC 中的BOOL與bool的區(qū)別

1.BOOL與bool的區(qū)別

  • bool 是0和1 分別對(duì)應(yīng) -> false/true
  • BOOL 是0和非0(-256~255除0外) 分別對(duì)應(yīng)->NO/YES
bool boolA = 1;
bool boolB = 0;
bool boolC = 256;
bool boolD = -1;
bool boolE = 13;


BOOL BOOLA = 1;
BOOL BOOLB = 0;
BOOL BOOLC = 256;
BOOL BOOLD = -1;
BOOL BOOLE = 13;


NSLog(@"boolA = %d",boolA);
NSLog(@"boolB = %d",boolB);
NSLog(@"boolC = %d",boolC);
NSLog(@"boolD = %d",boolD);
NSLog(@"boolE = %d",boolE);

NSLog(@"===========");

NSLog(@"BOOLA = %d",BOOLA);
NSLog(@"BOOLB = %d",BOOLB);
NSLog(@"BOOLC = %d",BOOLC);
NSLog(@"BOOLD = %d",BOOLD);
NSLog(@"BOOLE = %d",BOOLE);

結(jié)果輸出為:

boolA = 1  // true
boolB = 0  // false
boolC = 1  // true
boolD = 1  // true
boolE = 1  // true
===========
BOOLA = 1  // YES
BOOLB = 0  // NO
BOOLC = 0  // NO
BOOLD = -1  // YES
BOOLE = 13  // YES

由以上結(jié)果可看出:bool的結(jié)果只有兩個(gè):0和1。除了0是0,其他任意數(shù)字都是1,沒(méi)有大小限制。
BOOL的范圍為8位二進(jìn)制數(shù)字,如果超出了8位,就截取后8位。當(dāng)8個(gè)位置上都為0時(shí),返回0,所以0會(huì)返回0,256會(huì)返回0,但是257會(huì)返回1,因?yàn)橹唤厝『?位。


2.使用事項(xiàng)

  • bool
    Introduced to standard C in the C99 spec. (The C99 standard was published in 1999, but it took some years after that to become widespread in use.) Prior to that, "plain" C had no built-in Boolean type, so libraries that built on top of C often defined their own. (And often continued using their own types for source/binary compatibility even after they embraced C99 compilers.)
    Use this if you're writing ISO C and aren't working in the context of higher level libraries with their own Boolean types.

  • Boolean
    Defined by Carbon (the early-OSX-days compatibility bridge from the even older Mac Toolbox), which you might still see in some projects (due to transitive #include of headers that are really only around for compatibility with really old source code).
    Don't use this.

  • BOOL
    Defined by ObjC because NeXTSTEP needed its own Boolean type back in 1988. (The oldest objc.h I can find on my office bookshelf dates to 1992 and includes a definition of BOOL.)
    ObjC BOOL has often been defined as typedef signed char, meaning that it can hold more values than just YES (1) and NO (0). That can be a problem if you aren't careful. (Why even do that? Because if a type is one bit wide, it's hard to pack into well-aligned memory for good performance.)
    However, in iOS 64-bit (including tvOS) and watchOS, the compiler defines OBJC_BOOL_IS_BOOL, which makes ObjC BOOL just an alias for C99 bool. That means the language/compiler ensures that nonzero values are always stored as 1, so you don't have the issues that come from typedef signed char BOOL. (Still gotta worry about them on macOS or 32-bit iOS, though.)

  • TLDR
    If you're working in ObjC with ObjC frameworks (like Cocoa, UIKit, etc), you should use BOOL for consistency with the APIs you're interacting with. (Besides, YES and NO are much louder than true and false, and it's good to be emphatic when you're talking about absolute truth, right?)

DO NOT COMPARE TO YES

在條件判斷語(yǔ)句中,不要直接使用x == YES,或x != YES這種寫法


而要直接使用if (hasXXX) 或 if (isXXX) 這種寫法

還有就是避免將大于一個(gè)字節(jié)(8位二進(jìn)制)的值賦值給BOOL類型的變量,如BOOL a = 256

參考:
BOOL’s sharp corners
[Objc 中 “== YES” 的愚蠢行為有多可怕]
(http://www.itdecent.cn/p/75b88d2a0380)
is-there-any-difference-between-bool-boolean-and-bool-in-objective-c
iOS中BOOL跟bool的區(qū)別

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 起因 在技術(shù)群里發(fā)現(xiàn)有人在問(wèn) 這一段會(huì)輸出什么,問(wèn)這個(gè)的原因是他看到博客上都說(shuō)是輸出a no b yes,但是自己...
    Sunli_閱讀 4,823評(píng)論 1 9
  • OC中的BOOL類型占用了一個(gè)字節(jié),即是8位進(jìn)行表示。8位全是0時(shí)即是NO,8位之內(nèi)非0的就是YES,如果整數(shù)超過(guò)...
    Idoahc閱讀 3,935評(píng)論 3 5
  • 0 前言 5月8日提交的代碼在服務(wù)端編譯出現(xiàn)了錯(cuò)誤,而本機(jī)編譯過(guò)程中沒(méi)有任何問(wèn)題。定位到錯(cuò)誤日志,發(fā)現(xiàn)是因?yàn)殄e(cuò)把函...
    xohozu閱讀 10,361評(píng)論 2 6
  • 首先要理清一下關(guān)系: HTML:一些網(wǎng)頁(yè)控件。 超文本標(biāo)記語(yǔ)言,標(biāo)準(zhǔn)通用標(biāo)記語(yǔ)言(SGM或SGML)下的一個(gè)應(yīng)用。...
    goyohol閱讀 814評(píng)論 0 1
  • 曾經(jīng)的我們,已不是現(xiàn)在的我們,都為了各自的生活奔波,懷念那是的我們,一起瘋,一起笑,一起哭!同為對(duì)方加油,有心事會(huì)...
    唯伊楓葉閱讀 471評(píng)論 0 0

友情鏈接更多精彩內(nèi)容