如何避免“重新定义"和“重复协议"桥接头中的定义错误 [英] How to avoid "Redefinition" and "Duplicate Protocol" definition errors in Bridging Header

查看:55
本文介绍了如何避免“重新定义"和“重复协议"桥接头中的定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个 swift 类中使用一个 objc 库和一个 objc 类.所以我将以下内容放在 Bridging-Header.h 中:

I want to use a objc library and a objc class in a swift class. So I place the following in the Bridging-Header.h:

#import <FooLibrary/FooLibrary.h>
#import "FooClass.h"

问题是 FooClass.h 有

The problem is that the FooClass.h has

#import "FooLibrary.h".

因此,当我编译时,我收到了数百个错误,例如:重新定义枚举器"和属性具有先前的定义"以及重复协议定义"和Typedef 重新定义"

So when I compile I get hundreds of errors like:"Redefinition of enumerator" and "Property has previous definition" and "Duplicate protocol definition" and "Typedef redefinition"

如何避免这种情况?这似乎只是我遇到的一个愚蠢的心理障碍,但我无法克服它,所以我在这里问.

How do I avoid this? It seems like this is just a stupid mental block I am having but I can't get past it so here I am asking.

PartiallyFinite 建议我关注 #include

PartiallyFinite suggested I watch for #include

我进行了一个项目范围的搜索,但我根本没有使用它.图书馆里有几个.我选择了错误之一.装饰所在的文件永远不会包含在带有 #include

I did a project wide search and I and not using it at all. There are a few in the library. I chose one of the errors. The file the decoration is in is never included in any file with #include

推荐答案

听起来好像有什么事情导致预处理器相信 FooLibrary.h 标头是由第二个 #import<间接导入的/code> 不知何故与您在其上方包含的文件不同.我对什么的最佳猜测是,您的第一个框架样式导入引用了在构建期间复制到构建位置的头文件,而您的第二个直接文件导入引用了该头文件位于您的项目目录中,这意味着预处理器将它们视为两个完全独立的文件,导致它被导入两次.

Sounds like something is causing the preprocessor to believe that the FooLibrary.h header imported indirectly by the second #import is somehow not the same file as the one you include just above it. My best guess as to what is that your first, framework-style import is referencing header files that are copied to a build location during build, while your second, direct file import is referencing the header file as it is in your project directory, meaning that the preprocessor sees them as two completely separate files, resulting in it being imported twice.

建议的修复方法:

  1. 如果您可以使用框架样式的导入语法(如 #import <FooLibrary/FooClass.h>)包含 FooClass.h,那将可能解决问题.

  1. If you can include FooClass.h using the framework-style import syntax (like #import <FooLibrary/FooClass.h>), that will probably fix the problem.

如果您绝对确定 FooClass.h总是包含 FooLibrary.h,则您可以省略第一个完全导入,因为所有内容都将通过第二个间接导入.

If you're absolutely sure that FooClass.h will always include FooLibrary.h, you can probably just omit the first import entirely, as everything will get imported indirectly via the second one.

否则,您可以尝试一些不错的老式包含守卫(假设您对库头具有写访问权限):

Otherwise, you can try some nice, old-fashioned include guards (assuming you have write access to the library headers):

// FooLibrary.h

#pragma once // Maybe even throw in one of these for good measure;
             // since we're dealing with an obscure-sounding bug,
             // may as well try to fix it in all of the possible ways

#ifndef FOOLIBRARY_IMPORTED
#define FOOLIBRARY_IMPORTED
... // actual file contents
#endif

这将在第一次导入文件时定义一个预处理器宏,因此预处理器第二次尝试导入文件时,已定义的宏将阻止第二次导入内容.我不明白为什么 #import 在你的情况下没有这样做,因为这实际上是它相对于 #include 的唯一目的和优势,但如果这解决了它,¯\_(ツ)_/¯

This will define a preprocessor macro the first time the file is imported, so the second time the preprocessor tries to import the file, the already defined macro will prevent the contents from being imported a second time. I fail to understand why the #import isn't doing this in your case, as that's literally its only purpose and advantage over #include, but if this fixes it, ¯\_(ツ)_/¯

这篇关于如何避免“重新定义"和“重复协议"桥接头中的定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆