Reputation: 181
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 :
@objc
prefix keyword)#import "MyClass.h"
by @class MyClass;
in header files#import "MyProject-Swift.h"
in implementation files (.m) needing itThis 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 :
#import "MyProject-Swift.h"
at the beginning, nothing elseReplacing #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).
Upvotes: 10
Views: 15071
Reputation: 9039
This issue has randomly appeared and disappeared for our two projects (iOS & MacOS) each with several Targets and which share a lot of core Objective-C code, even though they are properly configured as per Apple docs for mixing Obj-C and Swift.
Like the OP we are converting these projects over to Swift class by class and we periodically run into the issue and waste hours trying to resolve it. Finally we tracked it down and figured out how to resolve it.
These are the key points to understand so as to permanently to resolve it:
@class myClass;
for anything that was already re-factored into Swift.import
of the Bridging Header to the Obj-C file with the implementation, typically the .m file.Simply and methodically go through each file complaining it can't find the imported bridging header and follow the above steps and voila, problem gone forever!
Upvotes: 0
Reputation: 1156
If you have two targets and you keep getting compile/semantic errors for newly edited swift files used in ObjC code, this could be the problem -
You got two <ProjectName>-swift.h
files. Like in mycase:
MyProject-swift.h
and MyProjectSandbox-swift.h
These are the files with all the swift classes and methods.
(These open up when you Command-click on any swift class from Obj-C code).
So I am working on the Sandbox
target.
I found that, when I made changes to swift files and clean-build, only MyProjectSandbox-swift.h
was being updated.
But the main problem was the ObjC code was referring to MyProject-swift.h
file which does not update as I was only rebuilding the (sandbox) target I was working on..
TL;DR;
So the solution is to make sure you clean and rebuild BOTH your targets so that both (MyProject-swift.h
and MyProjectSandbox-swift.h
) are updated with latest swift code and classes.
This will update both files and your ObjC code won't show any irrelevant compile errors for swift files.
Upvotes: 0
Reputation: 3233
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
Upvotes: 6
Reputation: 181
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.
Upvotes: 5
Reputation: 499
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.
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.
Upvotes: 5