objective-c 无法导入桥接头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26116288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to import bridging header
提问by Alan Scarpa
I've read a lot of questions and answers that deal with a similar issue, but I have yet to find a solution. If anyone could shed some light, that would be wonderful.
我已经阅读了很多处理类似问题的问题和答案,但我还没有找到解决方案。如果有人能透露一些信息,那就太好了。
I created a Swift project and now I want to combine it with some Objective-C. My "failed to import bridging header" only occurs when I attempt to #import my Chartboost.h file. So, as long as I don't have anything in my bridging header file, Xcode finds it and gives me no issue. But once I add this:
我创建了一个 Swift 项目,现在我想将它与一些 Objective-C 结合起来。我的“无法导入桥接头”仅在我尝试 #import 我的 Chartboost.h 文件时出现。因此,只要我的桥接头文件中没有任何内容,Xcode 就会找到它并且不会给我任何问题。但是一旦我添加了这个:
#import <Chartboost/Chartboost.h>
I get the error along with 38 other errors saying "Swift Compiler Error - Function definition not allowed here".
我收到错误以及其他 38 个错误,提示“Swift 编译器错误 - 此处不允许定义函数”。
I've correctly imported my framework. And my framework search path is correct. And it's only when I import the Chartboost framework. UIKit and Foundation work fine.
我已经正确导入了我的框架。我的框架搜索路径是正确的。只有当我导入 Chartboost 框架时才会这样。UIKit 和 Foundation 工作正常。
Here is what I did leading up to the issue....First, I created a new Obj-C file and then clicked "Yes when Xcode gave me a pop-up asking if it could configure a bridging header. This created "FunFacts-Bridging-Header.h"
这是我导致问题的原因......首先,我创建了一个新的 Obj-C 文件,然后在 Xcode 给我一个弹出窗口询问它是否可以配置桥接头时单击“是”。这创建了“FunFacts -Bridging-Header.h”
Then I made sure Objective-C Bridging Header path was correct under Swift Compiler - Code Generation.
然后我确保在 Swift Compiler - Code Generation 下 Objective-C Bridging Header 路径是正确的。
I even put in a very specific path /Users/me/Desktop/FunFacts/FunFacts-Bridging-Header.h and it still says "Failed to import".
我什至输入了一个非常具体的路径 /Users/me/Desktop/FunFacts/FunFacts-Bridging-Header.h 并且它仍然显示“导入失败”。
I've also set Defines Module to "Yes" (because I heard that may help). And my product module name is FunFacts.
我还将定义模块设置为“是”(因为我听说这可能会有所帮助)。我的产品模块名称是 FunFacts。
Why is FunFacts-Bridging-Header.h failing to import when I try to add #import ?
当我尝试添加 #import 时,为什么 FunFacts-Bridging-Header.h 无法导入?
采纳答案by Alan Scarpa
I figured out 2 solutions!
我想出了2个解决方案!
1) This isn't the prettiest way to do it, but I copy and pasted all my code from my Chartboost.h file into my Bridging-Header.h file instead of importing . This worked. But I knew there was a better way, so I kept hunting...
1) 这不是最漂亮的方法,但我将所有代码从 Chartboost.h 文件复制并粘贴到 Bridging-Header.h 文件中,而不是导入 . 这奏效了。但我知道有更好的方法,所以我一直在寻找……
2) The correct solution, I believe, is what I did next. My project's (not target) Framework Search Paths was empty. So, I went ahead and added the path to the Chartboost SDK like so: /Users/me/Desktop/Apps/SDKs/Chartboost
Now it builds and runs with no problem and I didn't have to copy and paste everything into the bridging header. All that was needed was
2)我相信正确的解决方案是我接下来所做的。我的项目(非目标)框架搜索路径为空。所以,我继续添加了 Chartboost SDK 的路径,如下所示:/Users/me/Desktop/Apps/SDKs/Chartboost
现在它构建和运行没有问题,我不必将所有内容复制并粘贴到桥接中标题。所需要的只是
#import <Chartboost/Chartboost.h>
If anyone is having a similar issue, just read what I did in my question, and then follow it up with this answer.
如果有人遇到类似问题,请阅读我在问题中所做的工作,然后按照此答案进行跟进。
回答by xemacobra
I answered this in another post: Chartboost integration issues with XCode 6.1
我在另一篇文章中回答了这个问题:Chartboost 与 XCode 6.1 的集成问题
EXPLANATION:
解释:
It seems like some pods and libraries don't bother importing the basic frameworks as they expect your code to already have them. This doesn't work with Swift as the way to import frameworks changed. All you need to do is to add the frameworks needed in your bridging header file.
似乎某些 pod 和库不会费心导入基本框架,因为他们希望您的代码已经拥有它们。这不适用于 Swift,因为导入框架的方式发生了变化。您需要做的就是在桥接头文件中添加所需的框架。
ANSWER:
回答:
It depends on what errors the compiler throws.
If it complains about NSObject, NSString, etc... you need to add
#import <Foundation/Foundation.h>in the top of your bridging header file.
这取决于编译器抛出的错误。如果抱怨NSObject,NSString等等......你需要添加
#import <Foundation/Foundation.h>在你的桥接头文件的顶部。
If it complains about UIView, UIButton, etc... you need to add #import <UIKit/UIKit.h>in the top of your bridging header file.
如果抱怨UIView,UIButton等等......你需要添加#import <UIKit/UIKit.h>在你的桥接头文件的顶部。
回答by Hirokio
The problem like your's puzzled me. But I found a solution.
像你这样的问题让我很困惑。但我找到了解决办法。
#import <Foundation/Foundation.h>
You should put this code (↑) before your code. This is just my solution (↓).
您应该将此代码 (↑) 放在您的代码之前。这只是我的解决方案(↓)。
#import <Foundation/Foundation.h>
#import <Chartboost/Chartboost.h>
Good luck!
祝你好运!
回答by PacoGV
This is how I solved it (and works!):
这就是我解决它的方法(并且有效!):
- Create yourProjectName-Bridging-Header.h file at the root of your project
- Include in that file the .h of the classes you want to expose and use into your swift project
- Go to yourProject->Build Settings->Search Paths, and set to Yes the "Always Search User Paths" key.
- Set "User Header Search Paths" to your project root path.
- 在项目的根目录下创建 yourProjectName-Bridging-Header.h 文件
- 在该文件中包含要公开并用于 swift 项目的类的 .h
- 转到 yourProject->Build Settings->Search Paths,并将“Always Search User Paths”键设置为 Yes。
- 将“用户标题搜索路径”设置为您的项目根路径。
That's it.
就是这样。
Apparently, Xcode miss out on third party folders when they are copied into your project
显然,当第三方文件夹被复制到你的项目中时,Xcode 错过了
I am on Xcode 6.3 , swift 1.2.
我在 Xcode 6.3 上,swift 1.2。
回答by Christopher Wade Cantley
Had a nearly identical issue and found a solution that worked for me.
有一个几乎相同的问题,并找到了一个对我有用的解决方案。
My problem was that the Bridging Header was not in ALL my targets.
我的问题是桥接头不在我的所有目标中。
It was in my project but not my UnitTest target. So I added it to by my UITest and UnitTest and it started working without issue.
它在我的项目中,但不在我的 UnitTest 目标中。所以我将它添加到我的 UITest 和 UnitTest 中,它开始正常工作。
回答by Kalyan Trandafil
If you use CocoaPods this could save your time. 1) The first what you need to do is to check your Podfile it will be like:
如果您使用 CocoaPods,这可以节省您的时间。1) 您需要做的第一件事是检查您的 Podfile,它将如下所示:
target 'YourProjTests' do
inherit! :search_paths
end
target 'YourProjUITests' do
inherit! :search_paths
end
2) Open baseproj configuration and set for test targets correct Pods-Proj.debug , see attached image:
回答by Dipesh Patel
The answer is really very simple.
答案真的很简单。
Make sure you are adding your bridging header path in SWIFT_OBJC_BRIDGING_HEADER under the target section instead of the project section.
确保在目标部分而不是项目部分下的 SWIFT_OBJC_BRIDGING_HEADER 中添加桥接头路径。
回答by Karlygash Zhuginisova
If you have these lines in you bridging header:
如果您的桥接标题中有这些行:
#ifndef Bridging_Header_h
#define Bridging_Header_h
#endif /* Bridging_Header_h */
Just delete them, it will solve the problems of Foundation and UIKit.
删除它们就可以解决Foundation和UIKit的问题。
回答by JerryZhou
One case is that if import <Chartboost+Extention/Chartboost+Extention.h>,
一种情况是,如果import <Chartboost+Extention/Chartboost+Extention.h>,
The right way is to import <Chartboost**_**Extention/Chartboost+Extention.h>,
正确的方法是import <Chartboost**_**Extention/Chartboost+Extention.h>,
Just because Pod build will change the framework name of Chartboost+Extentioninto Chartboost_Extention
只是因为 Pod build 会把框架名Chartboost+Extention改成Chartboost_Extention

