Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何解决;重新定义枚举器“;来自不同objc框架的错误_Objective C_Paypal_Xcode4.2 - Fatal编程技术网

Objective c 如何解决;重新定义枚举器“;来自不同objc框架的错误

Objective c 如何解决;重新定义枚举器“;来自不同objc框架的错误,objective-c,paypal,xcode4.2,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];

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];
}
更新(以下是基于正确答案的解决方法):




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

在pp_工作区.h中:

extern const int PAYPAL_ENV_LIVE;
在pp_工作区.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\u workaround.h”
而不是
“PayPal.h”
,并使用
PayPal\u ENV\u LIVE
而不是
ENV\u 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();
}

我刚刚遇到了这个错误,构建之前的清理为我解决了这个问题。

在同一个.m文件中包含两个框架的标题时,您看到错误了吗?是的,在同一个文件-appdelegate-中,我实际上需要初始化两个库并连接到live或prod服务器。您的建议是什么“并非所有的东西都能工作相同”?我应该注意什么?@pulkitsinghal几个C构造需要编译时常量。例如,如果需要声明数组
int vals[ENV_MAX]
标准要求
ENV_MAX
是一个编译时常量。不过,编译器总是会告诉您有些东西坏了,所以如果一切都编译好了,您就没事了。非常感谢您保存了这张光盘。继续…我按照外部建议,遇到了架构i386的
未定义符号:_OBJC_CLASS_$\u AuthNet”,引用自:Shoppin_PalAppDelegate.o中的OBJC CLASS ref,CardProcessingUtils.o中的OBJC CLASS ref(可能您的意思是:_OBJC_CLASS_$\u AuthNetWorkaround)
顺便说一句,我决定在paypal上外挂authnetstuff@pulkitsinghal看起来您没有正确地将AuthNet库添加到项目中。如果您搜索(例如)Google,查找“架构i386的未定义符号”,您将看到关于如何解决此链接问题的多个建议;这是一个。
extern void connect_authnet();
#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();
}