简体   繁体   English

条件编译和Objective-C / Xcode

[英]Conditional Compilation and Objective-C/Xcode

So I am working on a learning project and I am trying to create a header file that contains a store of URL's so that you can just change a single flag to change from Debug to Production. 所以我正在研究一个学习项目,我正在尝试创建一个包含URL存储的头文件,这样你就可以只更改一个标志,从Debug更改为Production。 This is what I am trying to do with the compiler and it is clearly wrong. 这就是我试图用编译器做的事情,这显然是错误的。 I can't find any information on how to do this in Objective-C, so that's why I came here. 我找不到有关如何在Objective-C中执行此操作的任何信息,这就是我来到这里的原因。

#define DEBUG 1
#if DEBUG
  NSString *URL = @"dev.myserver.com";
#else
  NSString *URL = @"myserver.com";
#endif

@interface GlobalURLRefrences : NSObject {
  //NSString *URL; removed this
}

//@property (nonatomic, retain) NSString *URL; removed this

@end

Now I am not sure if I need to declare that as a property or not. 现在我不确定是否需要将其声明为属性。 Also, once this is compiled properly, how to I access it from an outside class (of course after you #import the globalURL's class) Any sort of guidance on the proper method of doing this would be greatly appreciated. 此外,一旦正确编译,如何从外部类访问它(当然在#import globalURL的类之后)任何关于正确方法的指导将非常感激。

Geoff: I have a need for this kind of conditional in my Mac App Store app for validating receipts, and I do it with a separate build configuration and a -D flag. Geoff:我需要在我的Mac App Store应用程序中使用这种条件来验证收据,我使用单独的构建配置和-D标志来完成。 In Debug configuration, add a compiler flag something like -DDEBUG_BUILD (Note the double D at the beginning and no space.) and then use 在Debug配置中,添加一个类似-DDEBUG_BUILD的编译器标志(注意开头的双D而没有空格。)然后使用

#ifdef DEBUG_BUILD
    #define SERVER_URL_STRING @"http://dev.myserver.com"
#else
    #define SERVER_URL_STRING @"http://myserver.com"
#endif

This way, you don't have to remember to swap that #define each time you build for production. 这样,您每次构建生产时都不必记住交换#define (You'll forget eventually. Everyone has.) -If you do it this way, then you don't need the @property or the ivar declaration either.- Just saw you took these out already. (你最终会忘记的。每个人都有。) -如果你做这种方式,那么你就不需要@property或either.-刚刚看到你把这些已经出来了伊娃声明。

I think this should work 我认为这应该有效

#define DEBUG 1
#if DEBUG
   #define URL = @"dev.myserver.com";
#else
   #define URL = @"myserver.com";
#endif

I think you are trying to define the same variable twice. 我想你试图两次定义相同的变量。 How about this: 这个怎么样:

In your header: 在你的标题中:

#define DEBUG 1

In the init of your .m file: 在.m文件的init中:

#if DEBUG
   URL = @"dev.myserver.com";
#else
   URL = @"myserver.com";
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM