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

前端 未结 5 1192
轻奢々
轻奢々 2020-12-13 23: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 UnitTesti

相关标签:
5条回答
  • 2020-12-14 00:26

    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.

    0 讨论(0)
  • 2020-12-14 00:38

    @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
    
    0 讨论(0)
  • 2020-12-14 00:44

    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.

    0 讨论(0)
  • 2020-12-14 00:46

    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.

    0 讨论(0)
  • 2020-12-14 00:49

    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.

    0 讨论(0)
提交回复
热议问题