Swift Bridging Header import issue
Asked Answered
L

19

128

Following instructions, I've created a bridging header and added to my project. Unfortunately, the following error occurred:

:0: error: could not import Objective-C header '---path--to---header/....h'

Screenshot 1

In build settings I added header search path string to the location of bridging header, but nothing helped.

Has anyone experienced this problem?

Larock answered 10/6, 2014 at 16:51 Comment(7)
You could rather than add the header file yourself. Use the New File.. to add a new blank Objective-c .m file. This will make xcode offer you a header-bridge file where you can add your imports. You can then delete the .m file you just addedPedaiah
Yep, i've tried this way certainly too, but nothing helpedLarock
Is this on a fresh project or the same one you did by hand?Pedaiah
It's on fresh project, i've aded Keychain support wrapper, that i once wrote on OBJCLarock
Sorry. Not sure what you mean by the added Keychain support wrapper. But I notice you are on iOS do you get the same thing with an OSX project which is what I have been usingPedaiah
Didn't try OS X honestly, has anyone already successfully ported Obj-C code in swift via bridging header technique?Larock
I'm sure there must be or we world have heard more about it. I have partially done it in answering another question I used it to import a iTunes.h file for scripting bridge. But I have not fully tested it.Pedaiah
R
155

Be careful to add the file to the folder that your error is complaining! I've made the same mistake, if you create the file from Xcode, it will go to the folder: Project->Project->Header.h

And Xcode is looking for Project->Header.h

That means you need to put the file inside your project folder (ProjectName->ProjectNameFolder)!

UPDATED: I'm not sure if I got what you mean, but try this to solve your problem:

  1. Delete all your bridging files that you created until now.
  2. Select the main folder of project and hit new file->iOS->Header file.
  3. Write your imports in the header file created.
  4. Select the project inside Xcode->Build Settings, type in search field: bridging and put in the key SWIFT_OBJC_BRIDGING_HEADER the name of your header file or the path to it!

If you follow this steps, your header file will be created at the correct location!

Reannareap answered 18/6, 2014 at 19:36 Comment(10)
Well, i just tried this method, the header is really visible, but, unfortunately, bridging Obj-C classes are still invisible ( after including them in bridging header )Larock
At first the answer says "Need to put the file at the same level of xcodeproj", at the end, it says "ensure the file is ...not in the same level of xcodeproj". Why?Shanly
I meant that the file must be located inside de subfolder of project, not at the same level as xcodeproj. Example: In the folder of your project you have project, project.xcodeproj and projecttests. The file must be inside project folder :)Reannareap
guys for me I had to ./PATHTOBRIDGINGHEADER/BRIDGINGHEADERPatterman
I tried everything but nothnig works for me. This is very irritating. I am working with xcode 8 and swift 3. Can you help meAerogram
I'm confused you say "That means you need to put the file at the same level of xcodeproj!" but after you said "Ensure that the file is located inside the main folder of the project not in the same level of xcodeproj!" which one is it?Gitagitel
@bakalolo, you're correct, I've edited the answer. You should put it inside your project folder. The important thing is to keep the build settings (Objective-C Bridging Header section) with the same path as your bridge file!Reannareap
AWESOME! WORKS LIKE A CHARM! :DTsosie
the mangled English here makes it completely impossible to figure out what's going on. I wish an editor would clean this up.Inconstant
doesn't mean a thing in xcode 9: SWIFT_OBJC_BRIDGING_HEADERInconstant
F
68

In my case this was actually an error as a result of a circular reference. I had a class imported in the bridging header, and that class' header file was importing the swift header (<MODULE_NAME>-Swift.h). I was doing this because in the Obj-C header file I needed to use a class that was declared in Swift, the solution was to simply use the @class declarative.

So basically the error said "Failed to import bridging header", the error above it said <MODULE_NAME>-Swift.h file not found, above that was an error pointing at a specific Obj-C Header file (namely a View Controller).

Inspecting this file I noticed that it had the -Swift.h declared inside the header. Moving this import to the implementation resolved the issue. So I needed to use an object, lets call it MyObject defined in Swift, so I simply changed the header to say

@class MyObject;
Firenew answered 4/11, 2014 at 7:27 Comment(2)
This is silly but moving it to implementation file worked for me too. Unfortunately I needed it in the header because my class implements a delegate defined in a swift file. But since obj-c doesn't care if my class actually implements the delegate when I pass it in, I'll settle with this. But ideally there'd be another wayExultation
You may also have to check the imports of the one you're bringing into the bridging header. If any of those imported classes have a reference to the "-Swift.h" file you'll have to move the declaration to the .m.Shifra
W
30

Find the path at:

Build Settings/Swift Compiler-Code Generation/Objective-C Bridging Header

and delete that file. Then you should be ok.

Whist answered 22/6, 2014 at 0:5 Comment(5)
You win. I wasted so much time on this. Simple and effective. Xcode is doing something screwy with this path, like appending it to $(SRCROOT) behind the scenes. I think having a space in my path could be part of the problem but this was the silver bullet.Teleran
how do you delete that file?Locution
In Xcode 8 - instead of "Code Generation" it is in Swift Compiler-General.Mohun
Thanks you save me... Thanks a lotInterlocutress
After deleting this I'm getting this error "Cannot find 'GeneratedPluginRegistrant' in scope"Highness
B
18

This will probably only affect a small percentage of people, but in my case my project was using CocoaPods and one of those pods had a sub spec with its own CocoaPods. The solution was to use full angle imports to reference any files in the sub-pods.

#import <HexColors/HexColor.h>

Rather than

#import "HexColor.h"
Butz answered 29/9, 2014 at 19:2 Comment(4)
What's the difference?Mango
This was the problem for me. I got "Failed to import Bridging header" and the "Could not find XYZ.h" errors. Making this change solved it for me on Xcode 6.4 and Swift 1.2.E
@danielgomezrico One statement points to the file in a subdirectory. The other points to a file that doesn't exist.Pedaias
The solution for this error for me when using CocoaPods was simply running pod install again after creating the new configuration.Indemnity
F
16

For me deleting the derived data fixed it , I noticed even if I check out from an old commit, the same issue happens.

You can reach that option form Window-> Projects .

Fridafriday answered 28/3, 2016 at 12:53 Comment(0)
C
10

"we need to tell Xcode where to look for the header files we’re listing in our bridging header. Find the Search Paths section, and change the project-level setting for User Header Search Paths, adding a recursive entry for the ‘Pods’ directory: Pods/** " http://swiftalicio.us/2014/11/using-cocoapods-from-swift/

Crossbar answered 15/9, 2015 at 13:25 Comment(0)
F
9

For me it was because I forgot to add it to the Target's Build Settings.

enter image description here

Flop answered 5/5, 2015 at 19:4 Comment(1)
This may help if you have only added it to the project's build settings.Kuvasz
A
6

I've also experienced this problem and sadly it is just a bug in the SDK + Xcode. I talked to an engineer at WWDC, about this and a few other problems I was having with CloudKit. These bugs will be addressed in the next seed of Xcode.

It's the fun part about using beta software.

Amin answered 10/6, 2014 at 16:55 Comment(3)
Yeah, it's frustrating. My team and I was working trying to figure out an NSURL problem with CloudKit and we couldn't figure out why we were getting errors, turns out it was a server side problem by apple. we couldn't even do anything. wasted so much time. haha.Amin
The issue can actually be resolved by placing the header in the correct location. What worked for me is calling the header Project-Bridging-Header.h and placing it in the root of my project folder tree (as a sibling of my main Xcode project file). See @renan-kosicki answerGuerin
Fixed in an Xcode update, this was caused because of an issue with the early betas.Amin
M
3

for others who have troubles to add swift class into objective-c project. this is what work for me :

  1. create NEW swift file. this will make xcode to prompt if you want xcode to create all settings for mix swift-objective-c project including brigde-header.h for you. press yes.
  2. now, add your existing swift files you want to use in your project.
  3. in the implementation file you are going to use the swift class add : #import "YOURPROJECTNAME-swift.h" . this file xcode create for you. if your xcode project is myProject then "myProject-swift.h"

and that's it. now create the swift class in your code like it was objective-c.

Mariko answered 1/2, 2015 at 10:35 Comment(0)
S
3

I imported in some files from bridgin header files from cocoapods not in a proper way.

Instead of importing

#import <SomeCocoaPod/SomeCocoaPod.h>

I wrote

#import "SomeCocoaPod.h"

And this was my HUGE mistake

Smectic answered 29/10, 2015 at 9:38 Comment(0)
T
2

Add a temporary Objective-C file to your project. You may give it any name you like.

Select Yes to configure an Objective-C bridging header.

Delete the temporary Objective-C file you just created.

In the projectName-Bridging-Header.h file just created, add this line:

'#import < GoogleMaps/GoogleMaps.h >'

Edit the AppDelegate.swift file:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    GMSServices.provideAPIKey("AIza....") //iOS API key

    return true
}

Follow the link for full sample

Torbart answered 16/11, 2015 at 21:30 Comment(0)
C
2

For me it was not selecting 'Copy items if needed' in destination path while adding the framework. Simply re-add the framework with this option selected.

Cowling answered 3/2, 2016 at 17:27 Comment(0)
S
1

After initial few days of struggle, I finally managed to successfully integrate Facebook signup to my iOS app. Here are the steps(I am assuming you have already installed Facebook SDK v4.1 or above in your machines):

  1. Add the Facebook frameworks - FBSDKCoreKit,FBSDKLoginKit under your project.
  2. Make no changes in the Build settings as FB SDK v4.1 and above doesn't need anymore bridging header files.
  3. Import the FBSDKCorekit, FBSDKLoginKit in ViewController.swift, AppDelegate.swift files
  4. Add informations in the pList as mentioned here

  5. Build your app. And wohoo! no compile time errors.

Siegler answered 26/7, 2015 at 17:45 Comment(1)
This answer would be better if you included details from the linked page. If the linked page changes or the link ceases to work, people reading this answer will not know what to do when they come across step 4.Guglielmo
E
1

I have the same issue for different reason , here is my case I build project that needs slide menu to be included , I am using SWRevealViewController lib to approach that

when I import the library files I add sub-folder(SWRevealViewController) under Supporting Files for .h && .m files , it fire two errors , cant import bridge and SWRevealViewController.h is not found .

How I fix it

when I move files to Supporting Files directly (delete sub-folder) , SWRevealViewController.m automatically added to Build Phases --> Compile Sources and issue is gone

enter image description here

Eduino answered 3/4, 2016 at 9:10 Comment(0)
L
1

Had similar issue that could not be solved by any solution above. My project uses CocoaPods. I noticed that along with errors I got a warning with the following message:

Uncategorized: Target 'Pods' of project 'Pods' was rejected as an implicit dependency for 'Pods.framework' because its architectures 'arm64' didn't contain all required architectures 'armv7 arm64'

enter image description here

So solution was quite simple. For Pods project, change Build Active Architecture Only flag to No and original error went away.

Lettering answered 18/8, 2016 at 17:53 Comment(0)
S
1

I experienced that kind of error, when I was adding a Today Extension to my app. The build target for the extension was generated with the same name of bridging header as my app's build target. This was leading to the error, because the extension does not see the files listed in the bridging header of my app.

The only thing You need to to is delete or change the name of the bridging header for the extension and all will be fine.

Hope that this will help.

Sophistication answered 3/11, 2016 at 13:28 Comment(0)
Z
0

I actually created an empty OSX Source Objective C file under the project (where all my swift files are).

I added the imports and then deleted the .m file.

Zoochemistry answered 5/6, 2015 at 16:7 Comment(0)
E
0

Amongst the other fixes, I had the error come up when I tried to do Product->Archive. Turns out I had this :

Objective-C Bridging Header
  Debug (had the value)
  Release (had the value)
    Any architecture | Any SDK (this was blank - problem here!)

After setting it in that last line, it worked.

Evieevil answered 15/10, 2015 at 13:32 Comment(0)
T
0

Set Precompile Bridging Header to No fix the problem for me.

Tyr answered 23/11, 2017 at 5:50 Comment(1)
Why the downvotes? It's a legit solution for some people. I've benchmarked doing iterative builds with this on and off on a large project and see no difference at all (Apple has seen up to 30% speedups), but the amount of time I lose clearing build files and building from scratch is substantialGrandfatherly

© 2022 - 2024 — McMap. All rights reserved.