Objective C 中#define 的含义是什么?

发布于 2024-09-11 22:26:07 字数 275 浏览 5 评论 0原文

我想问一个关于objective-C或者可能是C语言的问题。我想问下面的#define代码是什么意思?是不是很像声明一个变量?

#define kMountainNameString         @"name"
#define kMountainHeightString           @"height"
#define kMountainClimbedDateString      @"climbedDate"

I want to ask a question about the objective-C or may be the C language. I want to ask what is the meaning of the following code of #define? Is it like to declare a variable?

#define kMountainNameString         @"name"
#define kMountainHeightString           @"height"
#define kMountainClimbedDateString      @"climbedDate"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北音执念 2024-09-18 22:26:08

这是一个简单的文本替换宏。与 C、C++ 中的工作方式相同。

在 kMountainNameString 出现的地方,编译器将“粘贴”
@“姓名”。从技术上讲,这通过称为预处理器的机制发生在编译器之前。

It's a simple text substitution macro. Works the same as in C, C++.

Where kMountainNameString appears the compiler will "paste in"
@"name". Technically speaking this occurs before the compiler by a mechanism called the preprocessor.

萝莉病 2024-09-18 22:26:08

#define 是从 C 继承的预处理器指令,其形式

#define identifier [value]

为一般情况下,它用于告诉预处理器将代码中的所有 identifier 实例替换为给定文本在将其传递给编译器之前。标识符也可以定义为不带值的值,以用作编译器标志,以防止同一变量的多个定义,或在执行期间不会更改的机器详细信息上进行分支。例如,要根据处理器的体系结构将不同的代码传递给编译器,您可以执行以下操作:

#ifdef INTEL86
//some 32-bit code
#else
//some 64-bit code
#endif

在这些定义中分配值时,通常最好用括号括住该值,以便将其保留为一个单元,无论它存在于什么上下文中。

例如,#define FOO 3 + 7 对以下结果的影响与 #define FOO (3 + 7) 不同线,由于算术运算的顺序:

a = 3 * FOO

请参阅此链接了解更多详细信息一般预处理器指令或此链接了解更多关于 Objective C 的信息。

#define is a preprocessor directive inherited from C that takes the form

#define identifier [value]

In general, it is used to tell the preprocessor to replace all instances of identifier in the code with the given text before passing it on to the compiler. Identifiers can also be defined without values to be used as compiler flags to prevent multiple definitions of the same variables, or to branch on machine details that will not change during execution. For example, to pass different code to the compiler based on the architecture of your processor you could do something like:

#ifdef INTEL86
//some 32-bit code
#else
//some 64-bit code
#endif

When assigning values in these definitions, it's often a good idea to surround the value with parentheses so as to preserve it as one unit, regardless of the context it exists in.

For example, #define FOO 3 + 7 has a different effect than #define FOO (3 + 7) on the result of the following line, due to the order of arithmetic operations:

a = 3 * FOO

See this link for more details on preprocessor directives in general or this link for information more focused on Objective C.

泪之魂 2024-09-18 22:26:08

预处理器将所有出现的 kMountainNameString 替换为 @"name"
在编译开始之前。

The preprocessor replaces all occurences of kMountainNameString with @"name"
before compilation begins.

时光是把杀猪刀 2024-09-18 22:26:08

#define 是 C 和 C++ 语言中的预处理器指令。

它用于定义文本的预处理器宏。
#define 用于在其所在的整个文件中进行替换。

#define <macro-name> <replacement-string>

#define is the preprocessor directive in c and c++ language.

It is used to define the preprocessor macros for the texts.
The #define is used to make substitutions throughout the file in which it is located.

#define <macro-name> <replacement-string>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文