[Objective-C] BOOL类型

参考:http://code.tutsplus.com/tutorials/objective-c-succinctly-data-types--mobile-21986


Object C自己定义了布尔类型:BOOL。它可以取两个值:YES 和 NO

在 /usr/include/objc/objc.h 中可以看到,它们的值分别为 1 和 0,然而,任何大于0值都可以看做YES

#import <objc/objc-api.h>

typedef struct objc_class *Class;

typedef struct objc_object {
  Class isa;
} *id;

typedef struct objc_selector  *SEL;
typedef id      (*IMP)(id, SEL, ...);
typedef signed char   BOOL;

#define YES             (BOOL)1
#define NO              (BOOL)0

#ifndef Nil
#define Nil 0   /* id of Nil class */
#endif

#ifndef nil
#define nil 0   /* id of Nil instance */
#endif
NSLog()函数中BOOL类型的占位符是"%i",如:
BOOL isHuman = 127;
if (isHuman) {
    // This will execute.
    NSLog(@"isHuman is TRUE");
}
if (isHuman == YES) {
    // But this *won't* execute.
    NSLog(@"isHuman is YES");
}
其中的isHuman的条件满足,而isHuman == YES的条件不满足。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值