Importing Project-Swift.h into a Objective-C class...file not found

111,070

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

  1. Any class that you need to access in the .h file needs to be a forward declaration like this

@class MySwiftClass;

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

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c

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

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

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:

  1. Open Xcode preference and navigate to DerivedData folder
  2. Search for "swift.h" in finder
  3. 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
  4. If you found "xxx-swift.h" file, make sure your import statement contains the correct name.

how-to-locate-swift-file

Share:
111,070

Related videos on Youtube

daspianist
Author by

daspianist

Updated on July 08, 2022

Comments

  • daspianist
    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:

    1. In Define Modules, selected YES for the app.
    2. 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" inside ViewController.m, ViewController.h and AppDelegate.m but none of them has worked.

    What else am I doing incorrectly? Thanks for your help.


    Screenshot of settings:

    enter image description here


    Errors that I am presently encountering:

    enter image description here

    • wzhang84
      wzhang84 over 7 years
      Delete all the Derived Data in ~/Library/Developer/Xcode/DerivedData did the trick for me.
    • schrulnz
      schrulnz almost 7 years
      Here is how I solved the issue: stackoverflow.com/a/45338549/8102549
  • daspianist
    daspianist over 9 years
    Thanks 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 inside ViewController.m, but continue to get the "file not found" error, despite Cleaning the project each time after trying a combination. I have included a screenshot in the body of the question in case it helps.
  • daspianist
    daspianist over 9 years
    I 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 in ViewController.m.
  • ikocel1
    ikocel1 over 9 years
    I don't know if its possible to mix directly objc code and swift code in the same file.
  • daspianist
    daspianist over 9 years
    Thanks, 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
    Darius Miliauskas about 9 years
    I 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
    iutinvg over 7 years
    I 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
    Rob Norback over 7 years
    hi 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
    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
    palme about 7 years
    Thanks 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
    Daneo almost 7 years
    My 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
    Apoorv Khatreja over 6 years
    Thank 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
    Paulus over 6 years
    Be 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 the Objective C Generated Interface Header Name in the target build settings. Before correcting this I was getting a file not found on the import
  • Amit Raz
    Amit Raz over 6 years
    @ApoorvKhatreja how did you force them to do that? Wrote the file name explicitly?
  • Amit Raz
    Amit Raz over 6 years
    Where is this file located? I look around there and could not find anything
  • Sikander
    Sikander over 6 years
    in .h file forward declaration of the class without imporing .h file did the trick for me. Thanks
  • dvanoni
    dvanoni almost 6 years
    Note 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 is My-App, you would import My_App-Swift.h.
  • Alexander  Danilov
    Alexander Danilov almost 6 years
    But how to import it to .h file if i need swift class in objc header?
  • johnrubythecat
    johnrubythecat over 5 years
    Brilliant. Thank you for posting this. Some fool put the header in the appdelegate.h file and it worked 'sometimes.'
  • Itachi
    Itachi over 5 years
    I 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
    Chuck Krutsinger over 5 years
    Excellent 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
    matchifang over 5 years
    @holm51 sorry to ask this, but where is Define Module and Product Module Name?
  • Bhargav Rao
    Bhargav Rao over 5 years
    Please 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
    CyberMew about 5 years
    dashes - in the name will be converted to underscores _ as well
  • rmp251
    rmp251 almost 5 years
    Can someone clarify the first point? Where exactly does the forward declaration go?
  • ArielSD
    ArielSD over 4 years
    I'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.
    Lucas P. over 4 years
    Thank 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
    dklt about 4 years
    Thanks for posting an answer. Screenshots and precise point form immediately make me scroll back + looked + ended my search... problem solved !
  • mdkr
    mdkr about 4 years
    This saved my day.
  • iOS Lifee
    iOS Lifee about 4 years
    superb solution
  • KeithTheBiped
    KeithTheBiped almost 4 years
    This 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
    Erik over 3 years
    Not just spaces get converterd to "_" but also "-" aka minus
  • Haseeb Javed
    Haseeb Javed almost 2 years
    Love you man :)