开发者

What's the use of #ifdef and #endif processor directives in iPhone?

I want to know about the use of #ifdef, #ifndef and #endif and which case, have to used those conditionals and what's the use of it? Whats the difference between the #ifdef and #ifndef?

For eg:

#define MY_Global

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

or,

  #ifdef MY_Global   
  -(void) customMethod
  {
       // Do something开发者_如何学运维
  }
  #endif 

Which one is correct, whether it should write only inside method or it does works outside method?. I haven't used this one before. so please Explain me?

Thanks!


AFAIK, #ifdef = "if defined" and #ifndef = "if not defined". These conditionals are useful, for example if you want a certain code to be compiled only for the simulator, then you'd write something like:

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

Which means, when you compile for the simulator, the AccelerometerSimulation.h will be imported. If you are compiling on a device, it is ignored totally. Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜