Xcode does not find Swift bridging header file in some files

10,550

Solution 1

I finally came up with the solution on my own : the project.pbxproj file was kind of corrupted after some git merge I guess. Some files of the project were referenced twice in that file, so I deleted the ones I thought being bad (maybe randomly chosen is closer from the truth).

Now it's working like a charm.

In fact this had no effect on the project until I tried to migrate files to Swift !

It would be a great idea to have a tool or function in XCode to reset this project.pbxproj file to the current things we have.

Solution 2

  1. Add a header file to your project, named [MyProjectName]-Bridging-Header.h. This will be the single header file where you import any Objective-C code you want your Swift code to have access to.

  2. In your project build settings, find Swift Compiler – Code Generation, and next to Objective-C Bridging Header add the path to your bridging header file, from the project’s root folder. So it could by MyProject/MyProject-Bridging-Header.h or simply MyProject-Bridging-Header.h if the file lives in the project root folder.

You only need one Bridging Header. Add your #import statements to this file, and your classes will now be available in your Swift code without any extra import statements.

Solution 3

basing on that asumption https://stackoverflow.com/a/31540611/2150954 I solved this problem in this simple way

Find objective C file, in which compiler claims that it can not find YourProject-Swift file there. Remove it file from project and then add.

After that my project successfully compiled and run

Share:
10,550
John S.
Author by

John S.

Updated on June 13, 2022

Comments

  • John S.
    John S. almost 2 years

    I do have a problem in my iOs application with Xcode. It has been coded in Objective-C, and now I am working on migrating files in Swift, one after another. The code is shared between team members with a versioning system (git).

    I started with the core classes and I proceed like this :

    • I write the new class in a Swift file, ex. MyClass.swift (with @objc prefix keyword)
    • I replace #import "MyClass.h" by @class MyClass; in header files
    • I add #import "MyProject-Swift.h" in implementation files (.m) needing it
    • I delete MyClass.h and MyClass.m files
    • I clean and build the project

    This process has worked several times, but for some reasons some #import "MyProject-Swift.h" do not work, and I get the error : 'MyProject-Swift.h' file not found.

    According to the different files causing the problem, sometimes, Ctrl+Click will open the "MyProject-Swift.h" file, sometimes not (no matter of the "not found error").

    But the file exists and the translated Swift code is present inside it.

    Another thing really weird, I tried to re-create new .h and .m files from those using #import "MyProject-Swift.h" stuff and having the problem, and after that it works (sometimes not, and I get other errors) !

    As it works in some cases, I really don't get why it is causing problems for other cases. Of course I searched among dozens of topics but did not find anyone with the same problem.

    Could it be an Xcode settings issue, or because of different Xcode versions between team developers ?

    Any idea ?

    Thanks

    Edited :

    I complete the issue description with another point of view.

    I do have the project in a working state : uses some of my new Swift files, it builds and runs well (some Objective-C files use Swift files).

    I know a certain .m file having the issue :

    • I add #import "MyProject-Swift.h" at the beginning, nothing else
    • Xcode says 'MyProject-Swift.h' file not found, Ctrl+Click does not work, and the project won't build
    • But, if I click on the button with four little squares (top left of the editor), in the displayed menu I have "Includes", the "MyProject-Swift.h" is listed under it, and clicking on "MyProject-Swift.h" opens the file !

    Replacing #import "MyProject-Swift.h" by #import <MyProject-Swift.h> won't change anything.

    I also have this case sometimes : Ctrl+Click opens "MyProject-Swift.h", but Xcode says 'MyProject-Swift.h' file not found (it won't build as well).

  • John S.
    John S. almost 9 years
    Thanks for the quick reply. The file you mention is already created and setup. The aim here is to do the opposite : using Swift code in Objective-C. So this is not MyProject-Bridging-Header.h but MyProject-Swift.h causing issues.
  • John S.
    John S. almost 9 years
    I found this : github.com/truebit/xUnique but I could not make it work as expected
  • John S.
    John S. over 8 years
    The effect of this is to delete all references of the file in project.pbxproj (by deleting the obj-c file), and then recreate them (by recreating the obj-c file). So yes, this can be a solution if you have few files impacted.
  • John S.
    John S. over 8 years
    I found this one, working well : github.com/simonwagner/mergepbx, give it a pbxproj file in conflict, it will repair it (it scans your project source files).
  • Sohaib Siddique
    Sohaib Siddique about 3 years
    solved my problem, now this Objective-C Bridging header move to Swift Complier-General section