Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode does not find Swift bridging header file in some files

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).

like image 878
John S. Avatar asked Jul 20 '15 09:07

John S.


Video Answer


3 Answers

  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.

like image 137
Bhumesh Purohit Avatar answered Nov 15 '22 15:11

Bhumesh Purohit


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.

like image 30
John S. Avatar answered Nov 15 '22 15:11

John S.


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

like image 37
Nikolay Shubenkov Avatar answered Nov 15 '22 15:11

Nikolay Shubenkov