Importing Project-Swift.H into a Objective-C Class...File Not Found

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

Project-Swift.h file not found

Project-Swift.h is a file auto generated by Xcode on successful compilation of the project. Catch here is the word successful compilation If your project has any compilation error Project-Swift.h file will not be generated. So in a way it becomes a deadlock. Bestway comment out all the lines that have compilation error and then manage to get it compile without any errors. Only after that Project-Swift.h will be generated.

Additional information, Once the Project-Swift.h file is generated if you open it and if you happened to see that your swift class is not imported there thats because Project-Swift.h imports only the classes that extends from NSObject So plain Swift classes will not be imported.

ISSUE:

You need to import Project-Swift.h in .m file and not .h file. So modify your Blakey as

#import 
#import "Blakey.h"
#import "MyProject-Swift.h"

@implementation Blakey


- (void)createKeyPairForSeed:(NSString *)seed;
{

}

Finally remove #import "MyProject-Swift.h" from Blakey.h

@import Foundation;

@class KeyPair;

@interface Blakey: NSObject

- (void)createKeyPairForSeed:(NSString *)seed;

@end

import swift class in objective-c, -Swift.h file not found

Updated May 2018 Xcode 9.3


  1. Build Settings->Objective-C Generated Interface Header Name
    and set the value to YourModule-Swift.h (this is usually already set, this is the filename you need to import on .m file #import "YourModule-Swift.h"

(Example, Project named CData)

Example


  1. Same as Step 1, Go to Build Settings and search for "Defines Module", set both values to YES

  2. Create a class that extends NSObject on .swift file

Example


  1. Build the project again

  2. Import YourModule-Swift.h file on .m file (Please notice it's case sensitive, Mymodule !== MyModule)

Sample Image



Related Topics



Leave a reply



Submit