如何解决"重新定义调查员"来自单独的objc框架的错误

时间:2012-03-22 21:13:57

标签: objective-c paypal xcode4.2

AuthNet和PayPal移动支付库都定义了ENV_LIVE枚举器。这导致Xcode错误如:

Redefinition of enumerator 'ENV_LIVE' ...

在这样的情况下,人们无法简单地更改依赖框架的源代码,Objective-c语法或xcode配置中有哪些可用的解决方法?

最初:

#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];
}

更新(以下是我最终根据正确答案使用的解决方法):

#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

2 个答案:

答案 0 :(得分:4)

这是因为两个包含都发生在同一个编译单元中。您可以通过将其中一个枚举包含到单独的编译单元中来解决此问题,但代价是使该枚举器的值为非编译时常量(实际上,它们将成为全局变量)。

在pp_workaround.h中:

extern const int PAYPAL_ENV_LIVE;

在pp_workaround.m中:

#import "PayPal.h" // I'm completely making up the name of PayPal's header
// The import of "AuthNet.h" is missing

const int PAYPAL_ENV_LIVE = ENV_LIVE;

现在,您可以添加"pp_workaround.h"代替"PayPal.h",并使用PAYPAL_ENV_LIVE代替ENV_LIVE。并非所有内容都可以正常工作,但编译时错误应该消失。

编辑如果您的代码只允许在.m文件中导入冲突的标题,则可以通过将连接代码包装在另一个抽象层中来解决问题(而不是解决它)。你自己的,像这样:

在paypal_init.h中:

extern void connect_paypal();

在paypal_init.m中:

#import "PayPal.h"
#import "paypal_init.h"

void connect_paypal() {
    // Use ENV_LIVE from PayPal.h here
}

在authnet_init.h中:

extern void connect_authnet();

在authnet_init.m中:

#import "AuthNet.h"
#import "authnet_init.h"

void connect_authnet() {
    // Use ENV_LIVE from AuthNet.h here
}

在您的主文件中:

#import "authnet_init.h"
#import "paypal_init.h"

void init() {
    connect_paypal();
    connect_authnet();
}

答案 1 :(得分:0)

我刚刚遇到这个错误,并且在为我修复问题之前打扫干净。