Importing Project-Swift.h into a Objective-C class...file not found
Solution 1
I was running into the same issue and couldn't get my project to import swift into obj-c classes. Using Xcode 6, (should work for Xcode 6+) and was able to do it in this way....
- Any class that you need to access in the .h file needs to be a forward declaration like this
@class MySwiftClass;
- In the .m file ONLY, if the code is in the same project (module) then you need to import it with
#import "ProductModuleName-Swift.h"
Link to the apple documentation about it
Solution 2
If the Swift code is inside a Module (like in your case):
#import <ProductName/ProductModuleName-Swift.h>
If the Swift code is inside the project (mixed Swift and ObjC):
#import <ProductModuleName-Swift.h>
In your case, you have to add this line in the *.m file:
#import <NewTestApp/NewTestApp-Swift.h>
IMPORTANT: look at the "<" in the import statement
Solution 3
How I managed to import swift into objective-c:
- Defines Module set to YES (on project - not on target)
- Product Module Name set (on target - not on project)
-
In your ViewController.m import the swift code with:
#import "MyProductModuleName-Swift.h"
- Add a swift file to your objective-c project (File -> New -> Swift) and Xcode will create the bridging header from objective-c to Swift but this is crucial for making it work the other way around too - apparently.
For the last piece in this puzzle thanks to Swiftoverload for making me aware of actually adding a Swift file via Xcode GUI and not just dragging and dropping existing swift-files into my project for making it work: http://nikolakirev.com/blog/using-swift-in-objective-c-project
Solution 4
Using Xcode 8.2.1 and if you look at Project > Build Settings > Objective-C Generated Interface Header Name, there it shows only one header file named like Product-Swift.h
This means that instead of importing each modules separately from Objective-C .m file, using individual -Swift.h file, you just import one Product-Swift.h which aggregated all Swift modules.
I encountered the same problem by looking for traditional way of importing modules separately, but the current version of Xcode and Swift 3 changed it to use only one header for all module importing.
Solution 5
Spent an hour on this issue, following these steps will help you to understand what's missing:
- Open Xcode preference and navigate to DerivedData folder
- Search for "swift.h" in finder
- If you can not find any project-swift.h file, this file haven't been generated. You usually need to add @objc to one of your swift class and successfully build the app, only then will Xcode generate this file
- If you found "xxx-swift.h" file, make sure your import statement contains the correct name.
Related videos on Youtube
daspianist
Updated on July 08, 2022Comments
-
daspianist almost 2 years
I have a project that was started in Objective-C, and I am trying to import some Swift code into the same class files that I have previously written Objective-C in.
I have consulted the Apple docs on using Swift and Objective-C in the same project, as well as SO question like this, but still no avail: I continue to get the file not found error after putting in
#import "NewTestApp-Swift.h"
(NewTestApp
is the name of the Product and module).Here is what I have done so far:
- In
Define Modules
, selectedYES
for the app. - Ensured that the Product Module name did not have any space in it (see screenshot below question)
I have tried using
#import "NewTestApp-Swift.h"
insideViewController.m
,ViewController.h
andAppDelegate.m
but none of them has worked.What else am I doing incorrectly? Thanks for your help.
Screenshot of settings:
Errors that I am presently encountering:
-
wzhang84 over 7 yearsDelete all the Derived Data in
~/Library/Developer/Xcode/DerivedData
did the trick for me. -
schrulnz almost 7 yearsHere is how I solved the issue: stackoverflow.com/a/45338549/8102549
- In
-
daspianist over 9 yearsThanks for answering @ikocel1. Unfortunately these didn't work. I've tried variations of
<NewTestApp-Swift/NewTestApp-Swift.h>
,<NewTestApp/NewTestApp-Swift.h>
and<NewTestApp/ViewController-Swift.h>
all insideViewController.m
, but continue to get the "file not found" error, despiteCleaning
the project each time after trying a combination. I have included a screenshot in the body of the question in case it helps. -
daspianist over 9 yearsI have not yet entered any Swift code yet into
ViewController.m
. I have been only trying to import the headers first to try to get it to accept Swift code. I will be mix and matching both Objective-C and Swift code inViewController.m
. -
ikocel1 over 9 yearsI don't know if its possible to mix directly objc code and swift code in the same file.
-
daspianist over 9 yearsThanks, I guess this was the assumption that I had based on Apple's "Mix and Match" document.. but I guess not inside the actual file. Thank you!
-
Darius Miliauskas about 9 yearsI did not get why this answer was approved. Yes, it is correct following the documentation but practically does not work for me too. Even in the question it is obvious that the user use the syntax as it is recommended on the this answer but still gets an error.
-
iutinvg over 7 yearsI my case I faced the problem because had decided to postpone migration to Swift 3 offered by Xcode. As a result Xcode wasn't able to build Swift source files and didn't generate the headers to include.
-
Rob Norback over 7 yearshi peter, where can you find this file? Or is it autogenerated by xcode? Also, do you have to setup your podfile differently because of this change?
-
petershine over 7 years@RobNorback If I'm not wrong, you can just write 'import "Product-Swift.h" in Obj-C header/implementation file to see the effect, without doing anything extra. Make sure your project settings have every configuration enabled for Swift and Obj-C mixing. Also, I still haven't experienced an issue with Podfile. Please let me know if I'm seeing things differently.
-
palme about 7 yearsThanks for the answer. That led me into the right direction. I had the same issue but with a protocol so I had to add @protocol MySwiftProtocol instead of the import in my header file.
-
Daneo almost 7 yearsMy project was correctly set up, but I was unable to import this header using the
"ProductModuleName-Swift.h"
way. Thanks for the pointer! -
Apoorv Khatreja over 6 yearsThank you, I had spent hours trying to debug this, and the problem indeed was that each of the modules in my project was generating their own
-Swift.h
header file. Forcing them all (I have 5 targets) to the use a common header file fixed the issue for me. -
Paulus over 6 yearsBe careful if your productmodulename contains strange characters. Mine contained a minus and Xcode change the generated filename to use an underscore instead of a minus. Thus say my product module name was
pjh-test
, I needed to#import "pjh_test_Swift.h"
. I only picked this up after examining theObjective C Generated Interface Header Name
in the target build settings. Before correcting this I was getting afile not found
on the import -
Amit Raz over 6 years@ApoorvKhatreja how did you force them to do that? Wrote the file name explicitly?
-
Amit Raz over 6 yearsWhere is this file located? I look around there and could not find anything
-
Sikander over 6 yearsin .h file forward declaration of the class without imporing .h file did the trick for me. Thanks
-
dvanoni almost 6 yearsNote that if your product name has a
-
character in it, the name of the generated header file will have that converted to_
. e.g. if your product name isMy-App
, you would importMy_App-Swift.h
. -
Alexander Danilov almost 6 yearsBut how to import it to .h file if i need swift class in objc header?
-
johnrubythecat over 5 yearsBrilliant. Thank you for posting this. Some fool put the header in the appdelegate.h file and it worked 'sometimes.'
-
Itachi over 5 yearsI found a new way to find the correct generated header file name: go to your target -> Build Settings -> Search keywords
Objective-C Generated Interface Header Name
, bingo! -
Chuck Krutsinger over 5 yearsExcellent notes. I would add that the test target is its own module and will have its own header named "<#TestTargetName#>-Swift.h" and that any spaces in the target names will be converted to spaces.
-
matchifang over 5 years@holm51 sorry to ask this, but where is
Define Module
andProduct Module Name
? -
Bhargav Rao over 5 yearsPlease don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question and flag for undeletion.
-
CyberMew about 5 yearsdashes
-
in the name will be converted to underscores_
as well -
rmp251 almost 5 yearsCan someone clarify the first point? Where exactly does the forward declaration go?
-
ArielSD over 4 yearsI've done both of these things, and am still seeing the same issue. Do you think it has to do with the order?
-
Lucas P. over 4 yearsThank you for this! I changed my app's diplay name (not the target's) and the Swift header changed name as well. This was the only way I could find what was going on.
-
dklt about 4 yearsThanks for posting an answer. Screenshots and precise point form immediately make me scroll back + looked + ended my search... problem solved !
-
mdkr about 4 yearsThis saved my day.
-
iOS Lifee about 4 yearssuperb solution
-
KeithTheBiped almost 4 yearsThis definitely fixed it. As far as I can tell I shouldn't need to do this to get it to work, but this works, for sure.
-
Erik over 3 yearsNot just spaces get converterd to "_" but also "-" aka minus
-
Haseeb Javed almost 2 yearsLove you man :)