在Objective-C中#define后的冒号 [英] Colon after #define in Objective-C
问题描述
在Objective-C中,您可以使用#define
In Objective-C you can define macros using the #define
#define kSomeMacro 1024
然后使用该宏执行类似的操作...
and then use that macro for something like this...
if (kSomeMacro == 1024) {
....
}
但是,如果您定义的宏末尾带有冒号
However if you define your macro with a colon on the end
#define kSomeMacro 1024;
然后,if语句将不起作用.这背后的原因是什么?为什么在定义宏时放一个;
,编译器为什么不抱怨?
Then the if statement won't work. What is the reasoning behind this and why doesn't the complier complain if you put a ;
when defining the macro?
推荐答案
宏仅被其定义替换.当您#define k 1024;
并编写if(k==1024)...
时,编译器实际看到的是:
Macros are just replaced by their definition. When you #define k 1024;
and write if(k==1024)...
what the compiler actually sees is:
if(1024; == 1024) ...
无法编译.
编译器不会抱怨,因为有时您实际上可能想在宏中添加分号(;
称为分号,而不是冒号,即:
).
The compiler doesn't complain because sometimes you may actually want to add a semicolon (;
is called semicolon, not colon, which is :
) to your macro.
这篇关于在Objective-C中#define后的冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!