How to resolve "enumerator override" errors from separate objc structures

Both AuthNet and PayPal mobile payment libraries have enumerator ENV_LIVE. This leads to Xcode errors, for example:

Redefinition of enumerator 'ENV_LIVE' ... 

In such cases where it is not possible to simply change the source code of the dependent frameworks, what are some workarounds available in the objective-c syntax or xcode configuration?

INITIALLY:

 #import "PayPal.h" #import "AuthNet.h" ... // AuthNet [AuthNet authNetWithEnvironment:ENV_TEST]; // PayPal if (STATUS_COMPLETED_SUCCESS == [PayPal initializationStatus]) { [PayPal initializeWithAppID:@"APP-XXX" forEnvironment:ENV_SANDBOX]; } 

UPDATE (this is what I ended up using as a workaround based on the correct answer):

 #import "PayPal.h" @class AuthNet; #import "AuthNetWorkaround.h" ... [AuthNet authNetWithEnvironment:AUTHNET_ENV_TEST]; 

 extern const int AUTHNET_ENV_LIVE; extern const int AUTHNET_ENV_TEST; @interface AuthNetWorkaround : NSObject @end 

 #import "AuthNetWorkaround.h" #import "AuthNet.h" @implementation AuthNetWorkaround const int AUTHNET_ENV_LIVE = ENV_LIVE; const int AUTHNET_ENV_TEST = ENV_TEST; @end 
+4
source share
2 answers

This is because both inclusions occur in the same compilation unit. You can get around this problem by moving the inclusion of one of the enumerations into a separate compilation unit, due to the cost of the values โ€‹โ€‹of the constants that are not the compiler for the enumerator (in fact, they become global variables).

In pp_workaround.h:

 extern const int PAYPAL_ENV_LIVE; 

In pp_workaround.m:

 #import "PayPal.h" // I'm completely making up the name of PayPal header // The import of "AuthNet.h" is missing const int PAYPAL_ENV_LIVE = ENV_LIVE; 

Now you can include "pp_workaround.h" instead of "PayPal.h" and use PAYPAL_ENV_LIVE instead of ENV_LIVE . Not everything will work the same, but the compile-time error should disappear.

EDIT . If your code allows you to import conflicting headers only in your .m file, you can fix the problem (and not bypass it) by wrapping the connection code in an additional abstraction layer, like this:

In paypal_init.h:

 extern void connect_paypal(); 

In paypal_init.m:

 #import "PayPal.h" #import "paypal_init.h" void connect_paypal() { // Use ENV_LIVE from PayPal.h here } 

in the authnet_init.h file:

 extern void connect_authnet(); 

in the authnet_init.m file:

 #import "AuthNet.h" #import "authnet_init.h" void connect_authnet() { // Use ENV_LIVE from AuthNet.h here } 

In your main file:

 #import "authnet_init.h" #import "paypal_init.h" void init() { connect_paypal(); connect_authnet(); } 
+4
source

I just had this error, and a clean one prior to build fixed the problem for me.

0
source

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


All Articles