Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Added unit testing target to Xcode - failed to import bridging header won't go away

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.

like image 946
Andrew Avatar asked Feb 05 '15 12:02

Andrew


People also ask

What is a bridging header Xcode?

Xcode offers to create this header when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app. If you accept, Xcode creates the bridging header file along with the file you were creating, and names it by using your product module name followed by "-Bridging-Header. h" .

How do I set unit tests in Xcode?

To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.

How do I create a bridging header in Xcode?

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.


2 Answers

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.

like image 185
Victor Choy Avatar answered Oct 04 '22 00:10

Victor Choy


@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 
like image 38
mkkrolik Avatar answered Oct 04 '22 01:10

mkkrolik