大多数语言使用 true/false
关键字作为布尔值.我发现即使 Smalltalk 也在使用 true/false
.我知道 Objective-C 只是借用 Smalltalk 的概念,而不是语言本身,但我很好奇它为什么使用 YES/NO
而不是更广泛使用的 true/false代码>.有什么历史原因吗?
Most languages use the true/false
keywords for boolean values. I found that even Smalltalk is using true/false
. I know Objective-C is just borrowing concepts from Smalltalk, not the language itself, but I'm curious why it's using YES/NO
instead of the more widely-used true/false
. Is there any historical reason?
Objective-C 被设计为(并且仍然是)C 的严格超集.创建者非常努力地确保他们不会破坏与 C 的兼容性以任何方式.他们还试图让他们的修改更加明显,以便很容易分辨出代码的哪些部分使用了 Objective-C,哪些部分使用了普通 C.例如,用于表示 NSStrings 的 @
而不仅仅是使用引号.这允许纯 C 字符串与新字符串共存.
Objective-C was designed to be (and still is) a strict superset of C. The creators worked very hard to ensure that they did not break compatibility with C in any way. They also tried to make their modifications somewhat obvious so that it would be easy to tell which parts of the code use Objective-C and which parts use plain C. Case in point, the @
used to denote NSStrings rather than just using quotes. This allows plain C strings to coexist with the new ones.
C 已经有一个非正式的 TRUE/FALSE 宏系统.我怀疑 Objective-C 的设计者选择 YES/NO 宏是为了避免冲突,并明确代码实际上是 Objective-C.还要注意空"对象的 nil
用法,而不是仅仅修改旧 NULL 的行为.
C already had an informal system of TRUE/FALSE macros. I suspect the designers of Objective-C chose the YES/NO macros to avoid conflict and to make it obvious that the code is actually Objective-C. Notice also the usage nil
for the 'empty' object rather than just modifying the behavior of good old NULL.
这篇关于为什么 Objective-C 使用 YES/NO 宏约定而不是 true/false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!