Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

objective c - Failed to import bridging header

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:

#import <Chartboost/Chartboost.h>

I get the error along with 38 other errors saying "Swift Compiler Error - Function definition not allowed here".

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.

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"

Then I made sure Objective-C Bridging Header path was correct under Swift Compiler - Code Generation.

I even put in a very specific path /Users/me/Desktop/FunFacts/FunFacts-Bridging-Header.h and it still says "Failed to import".

I've also set Defines Module to "Yes" (because I heard that may help). And my product module name is FunFacts.

Why is FunFacts-Bridging-Header.h failing to import when I try to add #import ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I answered this in another post: Chartboost integration issues with 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.

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.

If it complains about UIView, UIButton, etc... you need to add #import <UIKit/UIKit.h> in the top of your bridging header file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...