【问题标题】:What's the use of #ifdef and #endif processor directives in iPhone?iPhone 中#ifdef 和#endif 处理器指令有什么用?
【发布时间】:2011-01-21 13:24:07
【问题描述】:

我想知道#ifdef、#ifndef 和#endif 的用法以及在哪种情况下必须使用这些条件句,它们有什么用? #ifdef 和 #ifndef 有什么区别?

例如:

#define MY_Global

#ifdef MY_Global
  // write some code (Inside the method)
#endif 

或者,

  #ifdef MY_Global   
  -(void) customMethod
  {
       // Do something
  }
  #endif 

哪一个是正确的,它应该只写在方法内部还是在方法外部工作?我以前没用过这个。所以请解释一下?

谢谢!

【问题讨论】:

  • 第二个意味着如果MY_Global没有被定义,customMethod将完全丢失

标签: iphone preprocessor using-directives


【解决方案1】:

AFAIK,#ifdef = “如果已定义”和 #ifndef = “如果未定义”。这些条件很有用,例如,如果您希望仅为模拟器编译某些代码,那么您可以编写如下内容:

    #if TARGET_IPHONE_SIMULATOR
    #import "AccelerometerSimulation.h"
    #endif

这意味着,当您为模拟器进行编译时,将导入 AccelerometerSimulation.h。如果你在设备上编译,它会被完全忽略。希望对您有所帮助。

【讨论】:

  • 实际上你可以写#if defined(NAME)#if !defined(NAME)来代替#ifdef NAME#ifndef NAME。这也让您可以一次性测试多个名称的定义:#if defined(NAME1) && defined(NAME2)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
  • 2012-09-28
  • 2019-01-20
相关资源
最近更新 更多