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

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


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


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


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