Objective C“#if”句法

发布于 2024-12-09 03:16:27 字数 306 浏览 0 评论 0原文

当我查看某些类时,我对“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 技术交流群。

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

发布评论

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

评论(3

娇女薄笑 2024-12-16 03:16:27

#if 等是预处理器指令。这意味着它们是在编译之前处理的,而不是在运行时处理的。例如,这在定义调试行为时非常有用,该行为仅在为 debug 而构建时编译,而不是 release:(

#if DEBUG
    #define ISRelease(x) [x release]
#else
    #define ISRelease(x) [x release], x = nil
#endif

代码由 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 for debug and not release:

#if DEBUG
    #define ISRelease(x) [x release]
#else
    #define ISRelease(x) [x release], x = nil
#endif

(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.

沙与沫 2024-12-16 03:16:27

#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.

半枫 2024-12-16 03:16:27

之前的答案涵盖了从调试到术语的 #if 用法,但错过了一个更常见的用法:#if 用于注释大块代码。当程序员需要删除已经包含注释的内容时,这非常有用。

#if 0
  a lof of C code with /* style */ comments included
#endif

我经常用这个。优于任何其他 #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.

#if 0
  a lof of C code with /* style */ comments included
#endif

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.

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