iOS - 'MyProject-Swift.h' file not found when running Unit Tests for Swift

前端 未结 10 924
北恋
北恋 2020-12-02 07:03

I am trying to setup Unit Testing for my project. It is an existing Objective-C app, that I have recently added one Swift class to. I have setup the \'MyProject-Swift.h\' an

相关标签:
10条回答
  • 2020-12-02 07:07

    After I tried out everything I could find on the topic, the thing that worked for me was actually running the app although it was still showing the 'ModuleName-Swift.h file not found' error.

    It went away and my app works perfectly fine. I guess I should have considered that earlier... The error keeps coming back, but after running the app it always just goes away again. So the issue is not really solved for me, but I can continue working on other topics for now...

    0 讨论(0)
  • 2020-12-02 07:08

    Strangely, I was seeing this same error, but only when targeting a device (not the simulator). Before running the test I would see the red exclamation point next to the import statement for "MyProjectNameTests-Swift.h".

    However, funny thing is, if I just go ahead and run the test anyway (despite this apparent build error), then during the build phase that happens thereafter, XCode actually does generate the "MyProjectNameTests-Swift.h" file, and the test runs just fine!

    So, at least in my case, there was no need for the other solutions here, evidently, although I believe they do work also.

    I should also note that I deleted my DerivedData directory prior to this, so maybe that is a step also worth trying.

    0 讨论(0)
  • 2020-12-02 07:10

    I couldn't get it to work by adding that filepath mentioned by other answers, but I realized the file where it was complaining wasn't even being tested. I just had to remove it from the test target using the Right Utilities Side Bar.

    0 讨论(0)
  • 2020-12-02 07:11

    Adding a .swift file to that target fixes issue on it.

    0 讨论(0)
  • 2020-12-02 07:14

    "MyProject-Swift.h" file is generated at following path:

    "$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources"

    I end up adding this to Header Search Paths for my Unit Test target.

    Also as @hyouuu pointed out about being the known issue, hopefully Apple will provide some good solution at their end. Until I believe we need to use this above solution.

    https://developer.apple.com/library/content/documentation/Xcode/Conceptual/RN-Xcode-Archive/Chapters/xc6_release_notes.html

    0 讨论(0)
  • 2020-12-02 07:16

    A simple

    @testable import MyProject
    

    has done the job for me.

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