Conditional Compilation and Objective-C/Xcode

后端 未结 3 1237
栀梦
栀梦 2021-01-03 14:52

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

相关标签:
3条回答
  • 2021-01-03 15:21

    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:

    #if DEBUG
       URL = @"dev.myserver.com";
    #else
       URL = @"myserver.com";
    #endif
    
    0 讨论(0)
  • 2021-01-03 15:27

    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. In Debug configuration, add a compiler flag something like -DDEBUG_BUILD (Note the double D at the beginning and no space.) and then use

    #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. (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.

    0 讨论(0)
  • 2021-01-03 15:31

    I think this should work

    #define DEBUG 1
    #if DEBUG
       #define URL = @"dev.myserver.com";
    #else
       #define URL = @"myserver.com";
    #endif
    
    0 讨论(0)
提交回复
热议问题