47

I added a new Test target to my Xcode project. My project has Swift code and Objective-C code, and has a bridging header. Upon either adding the bridging header to UnitTesting's build settings, or doing import MyTarget, I'm getting the error:

failed to import bridging header

I've tried:

  • Adding the bridging header to project, unit testing and main target's build settings.
  • Changing defines modules to Yes.
  • Moving the bridging header file to the root folder.

I've also tried making a sample project, which built and worked fine. Taking everything I have over into a clean new project isn't an option at this point.

shim
  • 9,289
  • 12
  • 69
  • 108
Andrew
  • 7,693
  • 11
  • 43
  • 81

6 Answers6

55

If you use CocoaPods as package manager, must set search path etc. Give a simple way,

Try adding this in your Podfile:

target 'YourProductTests' do
    inherit! :search_paths
    # Pods for testing
end

and pod install

It works for me.

If the above solution does not work for you, try setting manually:

  1. Click your Test target -> Build Setting-> tab: All & Combined -> Swift Compiler -Code Generation -> Objective-C Bridging Header : add your xxx-bridging-header

  2. Check "Search Path", set up value of Framework Search Path, Header Search Paths, Library Search Path according to your main target. Maybe some search path lose here, manually add again.

shim
  • 9,289
  • 12
  • 69
  • 108
Victor Choy
  • 4,006
  • 28
  • 35
21

@Victor Choy solution works for me, but I had to move test target inside product target like so:

target 'YourProduct' do

   # Pods for product

   target 'YourProductTests' do
      inherit! :search_paths
      # Pods for product testing
   end
end

This did not work for me:

target 'YourProduct' do
   # Pods for product
end

target 'YourProductTests' do
   inherit! :search_paths
   # Pods for product testing
end
mkkrolik
  • 1,200
  • 14
  • 21
10

I faced the same problem. I did the following and the issue of 'Failed to import bridging header' is solved.

Steps:

  1. Select your project -> Build settings -> Search for 'Defines module' -> give 'YES'
  2. Copy Objective-C bridging header path
  3. Select your test target -> Build setting -> Swift compiler - General -> Objective-C bridging header -> Give Bridging header path.
  4. Make sure 'Header Search Paths' of test targets contains all the headers in ios Targets. Add them if any of the headers are missing.
  5. Build.

Reference: Refer this link.

Ssrini
  • 101
  • 1
  • 3
7

This may be useful for someone: if the Unit Test target is added to an existing project which already includes CocoaPods and some Objective-C library pods.

Below steps solved the Failed to import bridging header issue.

  1. Select your Project -> Build Settings -> Search for 'Defines module' -> give 'YES'

  2. Copy Objective-C Bridging Header Path from 'YourProduct' target and paste it in the 'YourProductTests' target's bridging header path.

  3. Podfile should have 'YourProductTests' target inside 'YourProduct' and should include the _inherit! :search_paths_ like below

...

target 'YourProduct' do
   # Add Pods for your product here...

   target 'YourProductTests' do
      inherit! :search_paths
      # Pods for product testing, if any
   end
end
  1. Then perform 'pod install' from the root folder of the project. This fills the Header Search Paths for 'YourProductTests'.

  2. As a last step verify that both 'YourProduct' and 'YourProductTests' target's Header Search Paths should be similar.

Rishi
  • 743
  • 8
  • 17
6

At this point, I've never had to import MyTarget to get unit tests to work in Swift.

Common Solutions

  • I assume you tried it, but it wasn't clear if you added your bridging header to your app target and test target at the same time?
  • Another option, that may not be ideal, is to add a bridging header in your test target so that you actually have 2 bridging headers. They should look the same and would be a good test.
  • If using $(SRCROOT) to reference your bridging header path, ensure it is being evaluated to correct path.
  • If all else fails, you should do file diff of your .xcodeproj with the one of your working project and match any relevant values that might be different.

The bridging header system isn't perfect, but here are a few issues I've run into.

tfrank377
  • 1,858
  • 2
  • 22
  • 34
1

For me, on a project without Cocoapods, I had one place, indistinguishable from other similar tests, where this warning would just not go away. I had a bridging header for the tests, but it was empty.

What cleared it for me, was going to the tests target > Build Settings > Objective-C Bridging Header and hit the delete key (should clear the link to the header file).

bauerMusic
  • 5,470
  • 5
  • 38
  • 53