Objective C“#if”句法
当我查看某些类时,我对“pound if”或 #if
语法感到有点困惑。
例如:
#if someConstant == someNumber
do something
#elif
etc
与:
if (someConstant == someNumber)
do something
else if {
do more stuff
}
有什么区别,为什么使用 #if
?
I'm a little confused by the "pound if" or #if
syntax I see when I look at some classes.
For example:
#if someConstant == someNumber
do something
#elif
etc
versus:
if (someConstant == someNumber)
do something
else if {
do more stuff
}
what's the difference, and why use #if
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
#if
等是预处理器指令。这意味着它们是在编译之前处理的,而不是在运行时处理的。例如,这在定义调试行为时非常有用,该行为仅在为debug
而构建时编译,而不是release
:(代码由 Jeff LaMarche 的 blog。)
这样您就不需要在提交应用程序之前检查整个应用程序的代码并移除负载的调试代码。这只是使用这些指令的一个小示例。
#if
etc are preprocessor directives. This means that they are dealt with before compiling and not at runtime. This can be useful, for example, in defining debugging behaviour that only compiles when you build fordebug
and notrelease
:(Code courtesy of Jeff LaMarche's blog.)
This way you don't need to go through your entire application's code just before you submit your app and remove a load of debugging code. This is just one small example of the use of these directives.
#if
是一个预处理器指令。if
是一个语言构造。不同之处在于最终程序的编译方式。当您使用#if 时,该指令的结果就是最终程序将在这些行中包含的内容。当您使用语言构造时,传递给该构造的表达式将在运行时而不是编译时进行计算。
#if
is a preprocessor directive.if
is a language construct.The difference is in the way that the final program is compiled into. When you use
#if
, the result of that directive is what the final program will contain on those lines. When you use the language construct, the expression that is passed to the construct will be evaluated at runtime, and not compile-time.之前的答案涵盖了从调试到术语的
#if
用法,但错过了一个更常见的用法:#if
用于注释大块代码。当程序员需要删除已经包含注释的内容时,这非常有用。我经常用这个。优于任何其他 #if 和注释样式的优势 - 它确实不关心其中大部分损坏的语法。
Previous answers cover
#if
usages from debug to terminology and miss one more common usage:#if
is used to comment big chunks of code. This is useful when programmers needs to get rid of something which already contains comments.I do use this a lot. Advantage over any other
#if
and comment style - it really does not care about most of broken syntax within it.