Objective-C Macro

I am currently using three servers (deploy, live_testing and local). I use macros to define a series of domain locations:

#define __LIVE_TESTING // Here I chose what domain to use #ifdef __PRODUCTION #define DOMAIN @"http://192.168.10.228/rest/" #define DOMAINCOMET @"http://192.168.10.228/" #endif #ifdef __LIVE_TESTING #define DOMAIN @"http://192.168.10.229/rest/" #define DOMAINCOMET @"http://192.168.10.229/" #endif ... 

I am having a compiler problem related to overriding DOMAIN and DOMAINCOMET. Is there a way around these warnings?

Thanks in advance, Clinton

+6
source share
2 answers

#undef is your friend:

 #ifdef __LIVE_TESTING #if defined(DOMAIN) && defined(DOMAINCOMET) #undef DOMAIN #undef DOMAINCOMET #endif #define DOMAIN @"http://192.168.10.229/rest/" #define DOMAINCOMET @"http://192.168.10.229/" #endif 
+11
source

If you get override errors, you must define the macro more than once. If this code is the only place where DOMAIN and DOMAINCOMET , then it is possible that both of your control DOMAINCOMET set.

This can happen if both __PRODUCTION and __LIVE_TESTING defined for any value - even 0, since you use #ifdef to determine if they are defined, and not test the actual value that they are assigned.

For example, even:

 #define __PRODUCTION 0 #define __LIVE_TESTING 1 

will cause both blocks to be evaluated according to your code and thus cause an override error.

If you want these two to be mutually exclusive, you should check their meaning in this way:

 #if __PRODUCTION==1 #define DOMAIN @"http://192.168.10.228/rest/" #define DOMAINCOMET @"http://192.168.10.228/" #elif __LIVE_TESTING==1 #define DOMAIN @"http://192.168.10.229/rest/" #define DOMAINCOMET @"http://192.168.10.229/" #endif 
+1
source

Source: https://habr.com/ru/post/914803/


All Articles