iPhone项目常数
我希望我的项目中有一个常量可以在 Lite 和 Pro 版本之间进行更改。我认为这不是最好的方法,但我正在尝试:
在我的应用程序委托中添加一个常量
#define BUILD_PRO 1 //0 =>精简版,1 =>专业版
当我需要它时,我导入 appDelegate 并测试它:
#import "myAppDelegate.h"
然后稍后
<前><代码>#if (BUILD_PRO==1) NSLog(@"这是专业版"); #endif
问题是此代码在某些文件中有效,而在其他文件中无效。我还没有找到这种行为的任何解释;有人对此有解释吗?
从同一项目获得两个版本(专业版和精简版)的正确方法是什么?
I want to have a constant in my project to change between Lite and Pro version. I don't think it is the best way to do it, but I am trying to:
add a constant in my app delegate
#define BUILD_PRO 1 //0 => LITE, 1 => PRO
when I need it I import the appDelegate and test it:
#import "myAppDelegate.h"
then later
#if (BUILD_PRO==1) NSLog(@"this is pro version"); #endif
The problem is that this code works in some files and don't works in others. I haven't found any explanation for this behaviour; does anyone have an explanation for it?
What is the right way to have two versions (pro and lite) from the same project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。预处理器定义就是实现这一点的方法。
我想它在某些文件中起作用,而在其他文件中不起作用,因为有些文件可能不包含您的 myAppDelegate.h 文件,因此无法获取定义。我建议定义“Lite Version”和“Pro Version”目标,并在每个目标的构建配置中设置预处理器变量。
创建精简版目标后(只需选择“专业版”目标上的重复目标上下文菜单项即可创建精简版):
这样您就不必更改任何头文件,只需构建精简版或完整目标即可。如果您需要在产品中的任何位置添加专业功能,只需使用:
Yep. A pre-processor definition is the way to do it.
I imagine it is working in some files and not others because some might not be including your myAppDelegate.h file and therefore not getting the definition. I suggest defining a "Lite Version" and "Pro Version" target and setting the pre-processor variable in the build configuration for each target.
Once you have created a lite target (just select the duplicate target context menu item on your "Pro Version" target to create the lite one):
That way you don't have to change any header files, you just need to build either the lite or full target. If you need to add pro functionality anywhere in your product just use:
实现此目的的一种方法是为专业版设置一个目标,为精简版设置一个目标。然后,您可以在专业版的预处理器宏下的构建设置中声明常量。
然后在您的代码中您可以执行以下操作:
One way of doing this is to have a target for the pro version and a target for the light version. Then you declare your constants in the build settings under Preprocessor Macros of the pro version.
Then in your code you can do:
我在 AppDelegate.m 的标头中声明一个变量:
然后在 AppDelegate 的 applicationDidFinishLaunching 中,我调用:
I declare a variable in the header of AppDelegate.m:
And then in applicationDidFinishLaunching in my AppDelegate, I call: