Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 668
Alan Scarpa Avatar asked Sep 30 '14 08:09

Alan Scarpa


People also ask

How do I import a bridging header in Swift?

Alternatively, you can create a bridging header yourself by choosing File > New > File > [operating system] > Source > Header File. Edit the bridging header to expose your Objective-C code to your Swift code: In your Objective-C bridging header, import every Objective-C header you want to expose to Swift.

How do I add a header to a bridge file?

To create an Objective-C bridging header file, all you need to do is drag some Objective-C code into your Swift project – Xcode should prompt you with the message "Would you like to configure an Objective-C bridging header?" Click "Creating Bridging Header" and you'll see a file called YourProjectName-Bridging-Header.

How do you use bridging headers in framework?

As the error states, bridging headers are not allowed in Frameworks. The Importing Code from Within the Same Framework Target section of the Mix & Match apple documentation hints at this. As they say, you need to "In your umbrella header file, import every Objective-C header you want to expose to Swift".


1 Answers

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.

like image 149
xemacobra Avatar answered Sep 19 '22 16:09

xemacobra