BOOL与bool的区别

bool是c++上的,而BOOL时Obejctive-C的。

看源码定义:

/// Type to represent a boolean value.
#if (TARGET_OS_IPHONE && __LP64__)  ||  TARGET_OS_WATCH
#define OBJC_BOOL_IS_BOOL
1
typedef bool BOOL ;
#else
#define OBJC_BOOL_IS_CHAR
1
typedef signed char BOOL ;
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
// even if -funsigned-char is used.
#endif

#if __has_feature(objc_bool)
#define YES __objc_yes
#define NO  __objc_no
#else
#define YES ((BOOL)
1 )
#define NO  ((BOOL)
0 )
#endif

即在iOS64位或Apple Watch上,BOOL跟bool一样是一个整型,如果在Mac OS X或iOS 32位则是一个char。

可以验证,运行以下代码,看输出

    NSLog ( @"YES:%ld" , sizeof ( YES ));   //1
  NSLog ( @"true:%ld" , sizeof ( true ));  //4

你可能感兴趣的:(iOS,开发)