开发者

Conditional compilation in Xcode

开发者 https://www.devze.com 2023-02-06 15:11 出处:网络
I would like to \'turn off\' the compiler for a section of my code.I do not want to use comments to \'hide\' the code from the compiler because there are a lot of /*...*/ comments embedded in this sec

I would like to 'turn off' the compiler for a section of my code. I do not want to use comments to 'hide' the code from the compiler because there are a lot of /*...*/ comments embedded in this section. I would guess that there is a common way to use compiler directives or #defines or something to control the compilati开发者_运维技巧on. In fact my desire to suppress compilation is not dependent on a condition like the SDK or the platform, I would just like to turn it off. How does one accomplish this?


A quick fix is to wrap that section of code with

#if 0
…
#endif

where 0 means false. To enable it again,

#if 1
…
#endif

Another option is to define a macro (Project Info -> Build -> Preprocessor macros) and define it when you want to disable that code, and undefine it when you want to enable that code. For instance,

#ifndef IGNORE_THIS_SECTION
…
#endif

You can achieve a similar, ‘inverse’ effect by using #ifdef instead.


You could create a preprocessor flag that is checked at compile time. I use this for multiple targets.

Step 3 Writing Preprocessor Codes, of this tutorial for creating multiple targets:

http://just2us.com/2009/07/tutorial-creating-multiple-targets-for-xcode-iphone-projects/

tells you how to create a flag and use the #if conditional compile.

0

精彩评论

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

关注公众号