objective-c viewcontroller - Swift to Objective-C header not created in Xcode 6




15 Answers

Now it works.

  1. Project must have a Product Module Name that does not include spaces.
  2. Defines Module must be set to Yes in Build Settings, under Packaging.

Finally works. Thanks to everyone for the help :-)

file swift.h

I have recently been working to add Swift to an existing project, to get to try it out in a real-world fashion.

Upon adding a Swift source file to the project, I have no problems about getting the "Bridging Header", that is, Objective-C to Swift.

But the -Swift.h header file that is supposed to expose Swift classes either marked @objc or subclasses of ObjC classes, is nowhere to be found :-(

I don't see any specific instructions on how to accomplish the usage of my new subclass, written in Swift, in my main app code (which is still Obj-C).

The app that I am lead developer of has a fairly large codebase (70.000 lines), so transitioning it in one go is out of the question.




I found that I had to fix all build errors before it would generate the file.

The problem for me was that it was a chicken/egg problem, in that I didn't see any build errors until I'd actually commented out the #import statement:

//#import "ProductModuleName-Swift.h"

which revealed a bunch of other errors in my Swift code.

Once I fixed these new errors and got the source building successfully, I uncommented out the #import and bingo! The header was created and importing correctly :)




If you're like me you've probably got the header name wrong. After bashing my head for a while I looked for the file in DerivedData and sure enough it's there. On my setup (using the standard derived data folder, I believe):

cd ~/Library/Developer/Xcode/DerivedData
find * -iname '*Swift.h'

Will find it. If nothing in that folder matches then Xcode is not generating it.

I'm using Xcode Version 6.2 (6C86e)




I wanted to add one more reason you might find an issue with this - I was creating a framework that mixed Swift and Objective-C code. I was not able to import the Swift classes outside the framework - I checked for the -Swift.h file and it was being generated but was empty.

The problem turned out to be very, very simple - I had not declared any of my Swift classes public! As soon as I added the public keyword to the classes, I was able to use them from classes inside and outside the framework.

Also of note, inside the framework (inside .m files only as another answer mentions) I had to import the -Swift.h file as:

#import <FrameworkName/FrameworkName-Swift.h>



Here is another variation of the moduleName-Swift.h not being generated.

I decided to include IOS Charts in my project but did not want to mingle the sources in the same directory, so I placed the Charts Project folder next to my code's project folder. I dragged the Charts project into my Project's Navigator Bar and included the framework in the my project target's Embedded Binaries list in the General project settings and set the Embedded Content Contains Swift Code switch to yes in my project's Build Settings tab in the Build Options section.

My project's moduleName-Swift.h file would never generate no matter what other switches or settings suggested here. Finally, using Lou Z's method of seeking out the -Swift.h files, I saw that a Charts-Swift.h file was being generated deep in my project's xcode Build directory in Charts.framework/Headers/

The solution to using Daniel Gindi's ios-charts Swift package without including the code in my project's source directory was to add:

#import "Charts/Charts-Swift.h"

To the modules charting my project's data.




Allow me to share my experiences trying to use Swift in an old objc project. I did not have to set Defines module to YES.

In my case I needed to manually make sure there was an objc Bridging Header. Only the generated interface header name was present in my build settings.

This lead to a MyApp-Swift.h file to being generated, but without any traces of my Swift classes.

The Apple documentation says that you will be prompted to create a bridging header when adding your first swift file. Well, I wasn't. I manually added a MyApp-Bridging-header.h file and pointed to it in the "Objective-C Bridging Header" field. That made my MyApp-Swift.h file become populated with my Swift classes.

Docs: Importing Swift into Objective-C




Seconding what a lot of people have here, but adding a pertinent screen shot. Swift and Obj-C code can certainly live together. It's not an all or none game.

To access Swift files in your Objective-C, all you need to do is add this call to your Obj-C file (in the .m / implementation file):

#import "{product_module_name}-Swift.h"

(Where {product_module_name} represents the product module name of your project). Rather than try to guess your product module name or figure out corner cases with spaces and special characters, just go to the build settings tab in the project and type in "product module name" - the inspector will reveal yours to you. Mine was something I did not expect it to be. Check out this screen shot if you're confused.

And to get Obj-c code working in Swift, you just need to add a bridging header file and import the relevant Obj-C headers there.




The most important thing is that This file is invisible!!! At least it is in Xcode6 beta5. There will be no such file named "YourModule-Swift.h" in your workspace. Just make sure you have module name and defines module set to yes, and use it in your Objective-C class.




You have to import a header in Objective-C classes, which is:

#import “ProductModuleName-Swift.h”

It is automatically generated, on the reference it says "Any Swift files in your target will be visible in Objective-C .m files containing this import statement."




Just a heads up for anyone who used "." in there project name. Xcode will replace the "." with an underscore "_" for the Swift version of the bridging header file. Oddly enough the Bridging-Header.h that is generated does not replace the periods with underscores.

For example a project with the name My.Project would have the following Bridging Header file names.

Bridging-Header.h (Autogenerated)

My.Project-Bridging-Header.h

Swift.h

My_Project.h

I hope this helps anyone who used a period and was stuck like I was. This file can be found at the following location.

Macintosh HD/Users/user/Library/Developer/Xcode/DerivedData/My.Project-fntdulwpbhbbzdbyrkhanemcrfil/Build/Intermediates/My.Project.build/Debug-iphonesimulator/My.Project.build/DerivedSources

Take care,

Jon




I found a trick that always works on me.

  1. Create your #import "ProductModuleName-Swift.h" in your appDelegate.h file and in your ProductName-Prefix.pch file. If you don't have it in xcode 6 you can create it with this way Why isn't ProjectName-Prefix.pch created automatically in Xcode 6?
  2. Command+shift+k to clean your code, if you receive an error about your "ProductModuleName-Swift.h" delete it from appDelegate.h file.
  3. Clean your code again. Now everything will work like a charm
  4. If you receive again error about the "ProductModuleName-Swift.h", now create again in appDelegate.h file and clean your code again.

Do this work (delete and create the "ProductModuleName-Swift.h" from appDelegate.h file and clean your code) everytime you receive this error to silent it.




I was having a hard time determining my module name/objective-c's import of swift's headers. I did read a lot of articles here too.

But the definitive answer for your project name with all its included special characters (be it '.' or a numeric or a space) - you can find the text that will work for you in the "Product Module Name" under the target's Build Settings.

For example my target name started with a numeric - "1mg" and the field mentioned above showed "_mg" as my module name.

so I used #import "_mg-Swift.h" and it worked.




If you were able to build a project before, with no issues related to “ProductModuleName-Swift.h” not found error, and now you are getting that nasty errors again, the reason might sit in your recent changes.

For me this was by (accidental) incorrect .swift file encoding. Reverting changes and bringing the back manually, does the job.




I had to delete WatchOS2 swift code from my Objective C project. And only after that XCode offered to generate -Swift.h




If you're using something like Cocoapods (and working out of the workspace rather than the project) try opening the project and building it before opening the workspace and building. YMMV.




Related

objective-c xcode swift