Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 723
daspianist Avatar asked Oct 11 '22 01:10

daspianist


People also ask

How do I import a Swift file into an Objective-C project?

Import Swift code into Objective-C within the same framework: Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .

How do I import into Objective-C?

To import a set of Objective-C files into Swift code within the same app target, you rely on an Objective-C bridging header file to expose those files to Swift. Xcode offers to create this header when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.

What is Swift H?

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.


2 Answers

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

like image 185
bolnad Avatar answered Oct 21 '22 00:10

bolnad


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

like image 97
ikocel1 Avatar answered Oct 20 '22 23:10

ikocel1