iPhone项目常数

发布于 2024-08-17 19:34:25 字数 450 浏览 6 评论 0原文

我希望我的项目中有一个常量可以在 Lite 和 Pro 版本之间进行更改。我认为这不是最好的方法,但我正在尝试:

  1. 在我的应用程序委托中添加一个常量

    #define BUILD_PRO 1 //0 =>精简版,1 =>专业版
    
  2. 当我需要它时,我导入 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:

  1. add a constant in my app delegate

    #define BUILD_PRO 1 //0 => LITE, 1 => PRO
    
  2. 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 技术交流群。

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

发布评论

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

评论(3

赴月观长安 2024-08-24 19:34:25

是的。预处理器定义就是实现这一点的方法。

我想它在某些文件中起作用,而在其他文件中不起作用,因为有些文件可能不包含您的 myAppDelegate.h 文件,因此无法获取定义。我建议定义“Lite Version”和“Pro Version”目标,并在每个目标的构建配置中设置预处理器变量。

创建精简版目标后(只需选择“专业版”目标上的重复目标上下文菜单项即可创建精简版):

  • 进入“项目/编辑目标”“专业版”菜单项
  • 转到“构建”选项卡并找到预处理部分(朝向底部)。
  • 将 BUILD_PRO=1 添加到“预处理宏”部分。

这样您就不必更改任何头文件,只需构建精简版或完整目标即可。如果您需要在产品中的任何位置添加专业功能,只需使用:

#ifdef BUILD_PRO
// do some pro stuff
#endif

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):

  • Go into the Project/Edit Target "Pro Version" menu item
  • Go to the build tab and find the Preprocessing section (towards the bottom).
  • add BUILD_PRO=1 to the "Preprocessing Macros" section.

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:

#ifdef BUILD_PRO
// do some pro stuff
#endif
陈年往事 2024-08-24 19:34:25

实现此目的的一种方法是为专业版设置一个目标,为精简版设置一个目标。然后,您可以在专业版的预处理器宏下的构建设置中声明常量。

然后在您的代码中您可以执行以下操作:

#ifdef BUILD_PRO
   //super awesome pro code here.
#endif

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:

#ifdef BUILD_PRO
   //super awesome pro code here.
#endif
べ映画 2024-08-24 19:34:25

我在 AppDelegate.m 的标头中声明一个变量:

int DEVICE_TYPE;

然后在 AppDelegate 的 applicationDidFinishLaunching 中,我调用:

- (void) setDeviceType {

  NSString* machineType = [[UIDevice currentDevice] machine];
  if ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"]) {
    DEVICE_TYPE = IPOD_TOUCH;
  } else if ([machineType isEqualToString:@"iPhone1,2"] || 
             [machineType isEqualToString:@"iPhone1,1"]) {
    DEVICE_TYPE = IPHONE3G;
  } else {
    DEVICE_TYPE = IPHONE3GS;
  }
}

I declare a variable in the header of AppDelegate.m:

int DEVICE_TYPE;

And then in applicationDidFinishLaunching in my AppDelegate, I call:

- (void) setDeviceType {

  NSString* machineType = [[UIDevice currentDevice] machine];
  if ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"]) {
    DEVICE_TYPE = IPOD_TOUCH;
  } else if ([machineType isEqualToString:@"iPhone1,2"] || 
             [machineType isEqualToString:@"iPhone1,1"]) {
    DEVICE_TYPE = IPHONE3G;
  } else {
    DEVICE_TYPE = IPHONE3GS;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文