Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift to Objective-C header not created in Xcode 6

People also ask

How do I add a Swift file to Objective-C project?

To access and use swift classes or libraries in objective-c files start with an objective-c project that already contains some files. Add a new Swift file to the project. In the menu select File>New>File… then select Swift File, instead of Cocoa Touch Class. Name the file and hit create.

How do I create an Objective-C bridging header in Xcode?

To create an Objective-C bridging header file, all you need to do is drag some Objective-C code into your Swift project – Xcode should prompt you with the message "Would you like to configure an Objective-C bridging header?" Click "Creating Bridging Header" and you'll see a file called YourProjectName-Bridging-Header.

How do I create an Objective-C bridging header in Swift?

Alternatively, you can create a bridging header yourself by choosing File > New > File > [operating system] > Source > Header File. Edit the bridging header to expose your Objective-C code to your Swift code: In your Objective-C bridging header, import every Objective-C header you want to expose to Swift.

Can I use Swift library in Objective-C?

The Swift library cannot be directly called from Objective-C, since it is missing the required annotations in the code, and in many cases, modules do not inherit from NSObject, rather they use the native Swift data types.


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 :-)


I had a similar problem and found that you can only add

#import "ProductModuleName-Swift.h"

to obj-c .m files, not .h files for the umbrella header to be found


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)


If your project module name has spaces in it, you must replace the spaces with an underscore.

For instance, if your project name is "My Project", you would use:

#import "My_Project-Swift.h"


* The only important thing is: *

to use the defined "Product Module Name" in the target, followed by -Swift.h

#import <Product Module Name>-Swift.h

// in each ObjectiveC .m file having to use swift classes
// no matter in which swift files these classes sit.

No matter if "Defines Module" param is set to Yes or No or if "Product Module Name" Project is not set.

Reminder: Swift classes must deriving from NSObject or been tagged with @objc attribute in order to be exposed to ObjectiveC / Foundation || Cocoa ...


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>