Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing Swift and Objective C : "ProjectName-Swift.h" file not found

So I'm working on an iOS project that contains Swift and Objective-C Classes.

To instantiate an object described in Objective-C, I've understand that I needed to have a Bridging-Header file that contains all the imports of the headers I will have to use on my Swift classes. And it works great on my project. I made all classes I needed, I subclassed Objective-C classes in Swift, everything is OK.

My problem is when it comes to do the opposite: instantiate a swift object in an Objective-C file. So I read that I need to have those options :

  • Define Modules as Yes
  • add the line #import "<#YourProjectName#>-Swift.h" on the *.m file I'm working on.

And that is what I did :

I changed all the values on the Build Settings that needed to be changed. I added the import line, I even tried to create the header file, but I still have the "<#YourProjectName#>-Swift.h" file not found error.

Of course, I replaced the YourProjectName by the actual module name, the one I found under Product Module Name in Packaging section on Build Settings. Also, there is no spaces in the Project Name.

Did I forgot a step ?

Thank you for your help.

like image 387
Que20 Avatar asked Feb 03 '15 13:02

Que20


People also ask

What is Swift H?

${Your project name}-Swift. h is for objc based project to use swift code I remember. It is generated by XCode, to translate swift code to a header file for objc to import. If you don't need to use swift in objc project, you don't need it.

Can we use OBJC files in Swift project?

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.

How import OBJC framework in Swift?

The correct approach is as follows: Import your Objective C framework by dragging and dropping the framework into an Xcode 6 Swift project. Create a new Objective C file in your project (File->New->File [Objective C for iOS]). Accept the prompt (agree) to create a bridging header file between Objective C and Swift.


1 Answers

Make sure you are importing the -Swift.h file from a .m Objective-C file and not a .h header file. The docs only talk about importing from .m:

... import the Xcode-generated header file for your Swift code into any Objective-C .m file you want to use your Swift code from.

The -Swift.h file is generated during the build and I've found that importing it from a .h file always fails, even when I know the file exists. Since it's generated by the build, I'm guessing it's cleaned out when the compiler is sifting through the headers and regenerated once the .swift files are compiled.

like image 58
phatblat Avatar answered Oct 13 '22 00:10

phatblat