美文网首页
OC 中的BOOL与bool的区别

OC 中的BOOL与bool的区别

作者: 快乐捣蛋鬼 | 来源:发表于2019-08-23 18:00 被阅读0次

1.BOOL与bool的区别

  • bool 是0和1 分别对应 -> false/true
  • BOOL 是0和非0(-256~255除0外) 分别对应->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);

结果输出为:

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

由以上结果可看出:bool的结果只有两个:0和1。除了0是0,其他任意数字都是1,没有大小限制。
BOOL的范围为8位二进制数字,如果超出了8位,就截取后8位。当8个位置上都为0时,返回0,所以0会返回0,256会返回0,但是257会返回1,因为只截取后8位。


2.使用事项

  • 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

在条件判断语句中,不要直接使用x == YES,或x != YES这种写法


而要直接使用if (hasXXX) 或 if (isXXX) 这种写法

还有就是避免将大于一个字节(8位二进制)的值赋值给BOOL类型的变量,如BOOL a = 256

参考:
BOOL’s sharp corners
[Objc 中 “== YES” 的愚蠢行为有多可怕]
(https://www.jianshu.com/p/75b88d2a0380)
is-there-any-difference-between-bool-boolean-and-bool-in-objective-c
iOS中BOOL跟bool的区别

相关文章

  • OC 中的BOOL与bool的区别

    1.BOOL与bool的区别 bool 是0和1 分别对应 -> false/true BOOL 是...

  • OC 中 BOOL 和 bool 的区别

    说明: 最近在写OC时被问到,为什么我用true/false而不用YES/NO。原因是之前写其他语言时养成了习惯,...

  • OC中BOOL和bool的区别

    OC中的BOOL类型占用了一个字节,即是8位进行表示。8位全是0时即是NO,8位之内非0的就是YES,如果整数超过...

  • ios中BOOL和bool的区别

    ios中BOOL和bool的区别 说明:objective-c 中的BOOL 实际上是一种对带符号的字符类型(si...

  • iOS中BOOL跟bool的区别

    起因 在技术群里发现有人在问 这一段会输出什么,问这个的原因是他看到博客上都说是输出a no b yes,但是自己...

  • ios中BOOL和bool的区别

    ios中BOOL和bool的区别说明:objective-c 中的BOOL 实际上是一种对带符号的字符类型(sig...

  • BOOL和bool的区别

    一、1、类型不同BOOL为int型bool为布尔型2、长度不同bool只有一个字节BOOL长度视实际环境来定,一般...

  • BOOL/bool/Boolean的区别

    学iOS开发算起来应该是一年多的,但是在平常使用布尔值的时候总是随便一用,只知道它们就只有两种值1或者0而已,并没...

  • OC中BOOL和bool遇到的坑

     抛出问题:今天在做业务的时候,用到了键值编码(KVC赋值)来访问要存取的类的属性的setValue:forKey...

  • kkbox-ios-dev笔记(六) - 概念/Respon

    一些新手常常搞混的东西 bool与BOOL在 64 位操作系统上,OC 的 BOOL会直接等于定义的stdbool...

网友评论

      本文标题:OC 中的BOOL与bool的区别

      本文链接:https://www.haomeiwen.com/subject/pnntectx.html