Objective C 中#define 的含义是什么?
我想问一个关于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个简单的文本替换宏。与 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.
#define
是从 C 继承的预处理器指令,其形式为一般情况下,它用于告诉预处理器将代码中的所有
identifier
实例替换为给定文本在将其传递给编译器之前。标识符也可以定义为不带值的值,以用作编译器标志,以防止同一变量的多个定义,或在执行期间不会更改的机器详细信息上进行分支。例如,要根据处理器的体系结构将不同的代码传递给编译器,您可以执行以下操作:在这些定义中分配值时,通常最好用括号括住该值,以便将其保留为一个单元,无论它存在于什么上下文中。
例如,
#define FOO 3 + 7
对以下结果的影响与#define FOO (3 + 7)
不同线,由于算术运算的顺序:请参阅此链接了解更多详细信息一般预处理器指令或此链接了解更多关于 Objective C 的信息。
#define
is a preprocessor directive inherited from C that takes the formIn 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: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:See this link for more details on preprocessor directives in general or this link for information more focused on Objective C.
预处理器将所有出现的 kMountainNameString 替换为 @"name"
在编译开始之前。
The preprocessor replaces all occurences of kMountainNameString with @"name"
before compilation begins.
#define 是 C 和 C++ 语言中的预处理器指令。
它用于定义文本的预处理器宏。
#define 用于在其所在的整个文件中进行替换。
#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.