为什么 Objective-C 使用 YES/NO 宏约定而不是 true/false?

2023-10-05移动开发问题
1

本文介绍了为什么 Objective-C 使用 YES/NO 宏约定而不是 true/false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

大多数语言使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7

游戏中心 facebook 喜欢
Game center facebook like(游戏中心 facebook 喜欢)...
2024-08-12 移动开发问题
3

Objective-C++ 导入 C++ 类失败,未找到 cassert
Objective-C++ importing C++ class fails, cassert not found(Objective-C++ 导入 C++ 类失败,未找到 cassert)...
2024-08-12 移动开发问题
10