objective - vec中的所有布尔值

时间:2021-08-13 21:05:13

Possible Duplicate:
What values should I use for iOS boolean states?

可能重复:对于iOS布尔状态我应该使用什么值?

I believe there are something like 5 boolean types in iOS environment (which comes from C, C++ and Objective C).

我相信iOS环境中有5种布尔类型(来自C、c++和Objective C)。

  • _Bool
  • _Bool
  • bool
  • bool
  • BOOL
  • BOOL
  • boolean_t
  • boolean_t
  • Boolean
  • 布尔

And there are at least four pairs of values for them:

它们至少有四对值:

  • true, false
  • 真的,假的
  • TRUE, FALSE
  • 真的,假的
  • YES, NO
  • 是的,没有
  • 1, 0
  • 1,0

Which one do you think is the best (style wise) to use for iOS Objective C development?

在iOS Objective - C开发中,你认为哪一种(风格方面)是最好的?

Update 1

更新1

I mentioned a type "boolean". It looks like it doesn't exist. I removed it from the list and added _Bool.

我提到了一种类型“布尔”。它看起来不存在。我从列表中删除了它并添加了_Bool。

I am aware of typedef's for these types and values. The question is about style differences.

我知道这些类型和值的类型定义。问题是风格的差异。

6 个解决方案

#1


5  

iOS and OS X are mostly made of Cocoa, which use the boolean type BOOL with values YES/NO.

iOS和OS X主要由Cocoa构成,它们使用布尔类型BOOL,值为YES/NO。

bool
  • Defined by C++.
  • 定义的C + +。
  • A true boolean, guaranteed to be 0 or 1.
  • 一个真正的布尔值,保证为0或1。
_Bool
  • Defined by C99.
  • 由C99定义的。
  • A true boolean, guaranteed to be 0 or 1.
  • 一个真正的布尔值,保证为0或1。
  • If stdbool.h is included, bool is #defined as _Bool.
  • 如果stdbool。包括,bool是#定义为_Bool。
BOOL
  • Defined by the Objective-C runtime at /usr/include/objc/objc.h.
  • 由Objective-C运行时在/usr/include/objc/objc.h定义
  • A signed char in 32 bit. Values may be YES (0x01), NO (0x00), or anything in the range -127 to 128. YES/NO are defined at <Foundation/NSObjCRuntime.h>.
  • 一个32位的有符号字符。值可以是YES (0x01)、NO (0x00)或范围为-127到128的任何值。YES/NO在 中定义。
  • A bool in 64 bits, guaranteed to be 0 or 1.
  • 一个64位的bool,保证为0或1。
Boolean
  • Defined by Carbon at CFBase.h.
  • 由cfbase。h中的碳定义。
  • An unsigned char.
  • 一个无符号字符。
  • Values may be TRUE (0x01), FALSE (0x00), or anything in the range -127 to 128.
  • 值可以是TRUE (0x01)、FALSE (0x00)或范围为-127到128的任何值。
boolean_t
  • Defined by /usr/include/mach/i386/boolean.h
  • 由/usr/include/mach/i386/boolean.h定义
  • An int in x32 or unsigned int in x64.
  • x32中的整数或x64中的无符号整数。

For non true boolean types:

对于非真布尔类型:

  • Any non zero value is treated as true in logical expressions.
  • 任何非零值在逻辑表达式中被视为真值。
  • If you cast to a boolean types with less range than the casted type, only the lower bytes are used.
  • 如果您将数据转换为比已转换类型范围更小的布尔类型,则只使用较低的字节。

Cases where one type or another makes a difference are hard to imagine. There are several cases where casting to BOOL may bite you, and some rare situations (eg: KVO converts BOOL to a NSNumber, and bool to a CFBoolean). If anything, when you consistently use BOOL, you are covered in case Apple changes its definition.

很难想象一种或另一种类型会有什么不同。有几种情况下,对BOOL的强制转换可能会让你很难受,还有一些情况(例如:KVO将BOOL转换为NSNumber, BOOL转换为CFBoolean)。如果有的话,当你一直使用BOOL时,如果苹果改变了它的定义,你就会受到影响。

#2


3  

Use BOOL and YES/NO in Objective-C code. That's expected in Objective-C code and how the Objective-C headers define the data type. (Note that you may occasionally deal with other type/values, such as when checking the "truthiness" of pointers or when dealing with, e.g., C++ code, but Objective-C generally uses BOOL and YES/NO for boolean data types and values.)

在Objective-C代码中使用BOOL和YES/NO。这在Objective-C代码中是意料之中的,以及Objective-C头是如何定义数据类型的。(请注意,您可能偶尔会处理其他类型/值,例如检查指针的“truthiness”或处理诸如c++代码,但Objective-C通常会对布尔数据类型和值使用BOOL和YES/NO)。)

#3


2  

Use ObjC's BOOL for ObjC code. Use the others where they're "native". Anything else will tend to stand out as "unusual" and worthy of extra care when reading the code, which is a property that should be reserved for code that actually is unusual and worthy of extra care.

使用ObjC的BOOL用于ObjC代码。使用其他他们是“本地的”。在阅读代码时,其他任何东西都会显得“不同寻常”,值得特别注意,这是一个应该保留给代码的属性,它实际上是不寻常的,值得特别注意的。

#4


2  

It's basically absolutely indifferent. In practice, I bet they're even declared the very same way: typedef signed char BOOL;, typedef signed char Boolean;, etc.

它基本上是完全漠不关心。在实践中,我打赌它们甚至被声明为相同的方式:typedef签名的char BOOL;

So they're practically compatible and equivalent; the best approach is, however, to respect the type methods expect and return, so write

所以它们实际上是兼容并等价的;但是,最好的方法是尊重类型方法expect和return,因此编写

[object someObjectiveCMethod:YES];

instead of

而不是

[object someObjectiveCMethod:TRUE];

and

CFWhateverSetBooleanProperty(true);

instead of

而不是

CFWhateverSetBooleanProperty(YES);

#5


0  

You could use each one if wasn't for the fact that who reads the code may not know this type. So for convention, use BOOL which could have a value of YES (1) or NO .

如果不是因为读代码的人可能不知道这种类型,您可以使用每一个。因此,对于约定,使用BOOL,它可以有YES(1)或NO的值。

#6


0  

BOOL with YES and NO.

说“是”和“不是”。

However BOOL is signed char so YES equals to 1, and NO equals to 0.

但是BOOL是带符号的,所以YES等于1,NO等于0。


In objc.h they are defined as:

在objc。h定义为:

typedef signed char BOOL; 

And

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

#1


5  

iOS and OS X are mostly made of Cocoa, which use the boolean type BOOL with values YES/NO.

iOS和OS X主要由Cocoa构成,它们使用布尔类型BOOL,值为YES/NO。

bool
  • Defined by C++.
  • 定义的C + +。
  • A true boolean, guaranteed to be 0 or 1.
  • 一个真正的布尔值,保证为0或1。
_Bool
  • Defined by C99.
  • 由C99定义的。
  • A true boolean, guaranteed to be 0 or 1.
  • 一个真正的布尔值,保证为0或1。
  • If stdbool.h is included, bool is #defined as _Bool.
  • 如果stdbool。包括,bool是#定义为_Bool。
BOOL
  • Defined by the Objective-C runtime at /usr/include/objc/objc.h.
  • 由Objective-C运行时在/usr/include/objc/objc.h定义
  • A signed char in 32 bit. Values may be YES (0x01), NO (0x00), or anything in the range -127 to 128. YES/NO are defined at <Foundation/NSObjCRuntime.h>.
  • 一个32位的有符号字符。值可以是YES (0x01)、NO (0x00)或范围为-127到128的任何值。YES/NO在 中定义。
  • A bool in 64 bits, guaranteed to be 0 or 1.
  • 一个64位的bool,保证为0或1。
Boolean
  • Defined by Carbon at CFBase.h.
  • 由cfbase。h中的碳定义。
  • An unsigned char.
  • 一个无符号字符。
  • Values may be TRUE (0x01), FALSE (0x00), or anything in the range -127 to 128.
  • 值可以是TRUE (0x01)、FALSE (0x00)或范围为-127到128的任何值。
boolean_t
  • Defined by /usr/include/mach/i386/boolean.h
  • 由/usr/include/mach/i386/boolean.h定义
  • An int in x32 or unsigned int in x64.
  • x32中的整数或x64中的无符号整数。

For non true boolean types:

对于非真布尔类型:

  • Any non zero value is treated as true in logical expressions.
  • 任何非零值在逻辑表达式中被视为真值。
  • If you cast to a boolean types with less range than the casted type, only the lower bytes are used.
  • 如果您将数据转换为比已转换类型范围更小的布尔类型,则只使用较低的字节。

Cases where one type or another makes a difference are hard to imagine. There are several cases where casting to BOOL may bite you, and some rare situations (eg: KVO converts BOOL to a NSNumber, and bool to a CFBoolean). If anything, when you consistently use BOOL, you are covered in case Apple changes its definition.

很难想象一种或另一种类型会有什么不同。有几种情况下,对BOOL的强制转换可能会让你很难受,还有一些情况(例如:KVO将BOOL转换为NSNumber, BOOL转换为CFBoolean)。如果有的话,当你一直使用BOOL时,如果苹果改变了它的定义,你就会受到影响。

#2


3  

Use BOOL and YES/NO in Objective-C code. That's expected in Objective-C code and how the Objective-C headers define the data type. (Note that you may occasionally deal with other type/values, such as when checking the "truthiness" of pointers or when dealing with, e.g., C++ code, but Objective-C generally uses BOOL and YES/NO for boolean data types and values.)

在Objective-C代码中使用BOOL和YES/NO。这在Objective-C代码中是意料之中的,以及Objective-C头是如何定义数据类型的。(请注意,您可能偶尔会处理其他类型/值,例如检查指针的“truthiness”或处理诸如c++代码,但Objective-C通常会对布尔数据类型和值使用BOOL和YES/NO)。)

#3


2  

Use ObjC's BOOL for ObjC code. Use the others where they're "native". Anything else will tend to stand out as "unusual" and worthy of extra care when reading the code, which is a property that should be reserved for code that actually is unusual and worthy of extra care.

使用ObjC的BOOL用于ObjC代码。使用其他他们是“本地的”。在阅读代码时,其他任何东西都会显得“不同寻常”,值得特别注意,这是一个应该保留给代码的属性,它实际上是不寻常的,值得特别注意的。

#4


2  

It's basically absolutely indifferent. In practice, I bet they're even declared the very same way: typedef signed char BOOL;, typedef signed char Boolean;, etc.

它基本上是完全漠不关心。在实践中,我打赌它们甚至被声明为相同的方式:typedef签名的char BOOL;

So they're practically compatible and equivalent; the best approach is, however, to respect the type methods expect and return, so write

所以它们实际上是兼容并等价的;但是,最好的方法是尊重类型方法expect和return,因此编写

[object someObjectiveCMethod:YES];

instead of

而不是

[object someObjectiveCMethod:TRUE];

and

CFWhateverSetBooleanProperty(true);

instead of

而不是

CFWhateverSetBooleanProperty(YES);

#5


0  

You could use each one if wasn't for the fact that who reads the code may not know this type. So for convention, use BOOL which could have a value of YES (1) or NO .

如果不是因为读代码的人可能不知道这种类型,您可以使用每一个。因此,对于约定,使用BOOL,它可以有YES(1)或NO的值。

#6


0  

BOOL with YES and NO.

说“是”和“不是”。

However BOOL is signed char so YES equals to 1, and NO equals to 0.

但是BOOL是带符号的,所以YES等于1,NO等于0。


In objc.h they are defined as:

在objc。h定义为:

typedef signed char BOOL; 

And

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