Swift to Objective-C header not created in Xcode 6
Asked Answered
F

32

250

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 Objective-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.

Faubourg answered 5/6, 2014 at 14:17 Comment(19)
Yes it does. It's really asking where in the XCode IDE you need to do something to get this Swift->ObjC headed created, since it doesn't happen "by default" when adding Swift code to an existing ObjC project.Faubourg
No this really doesn't have anything to do with the xcode IDE, the xcode IDE tag is reserved for issues like, "Why does xcode just crash randomly? blah blah blah" not for issues that you are having within the xcode IDE just because you are using it doesn't mean it is the issue. I could go use another IDE that supports objective-c/swift development and I would be still suffering from the same issue. Think of it like this if you think you should be using the xcode tag - DON'TChev
@Chev I disagree. In this case, if you were editing the files and compiling from the command line, you would not be seeing this problem. It is the IDE that is performing (or is supposed to perform) on your behalf which is at issue. This is most likely related to project setup, which is definitely an IDE functionKee
Do you have multiple targets in your Project?Kee
@Kee in that case this has nothing to do with objective-c or swift-language as you both clearly state it is in relation to the IDE, so what has it got to do with the programming languages?Chev
@gaige: Yup, I do! A test target, and a non-test target. I added the Swift file to the target, and the -Bridging-Header.h shows up.Faubourg
@Chev it is the intersection of all three-it is a problem directly related to how Objective-C and Swift interact within the Xcode IDE. If the system required a single tag, I would suggest Xcode IDE, but since it allows more expression, it seems that this usefully describes what this issue describes.Kee
@Chev From the docs (PDF): When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. (My boldfacing, Apple's italics.) This is clearly a question about a feature of Xcode that helps you use Swift and Objective-C together, and all three tags are surely appropriate.Grillwork
@Grillwork thanks to the backing up of documentation I will back down and withdraw my comments. However I only do so because of the backup of documentation.Chev
@DavidKristensen I'm getting a similar problem on a multi-target (8 in my case) project, where it auto-generates for some and not others. I have a target whose name has a space in it, and that doesn't auto-generate. Other targets work fine. (Note: product name doesn't contain a space, just the target name)Kee
@gaige: That's interesting. My target name -does- include a space. I'll rename it and try again! Thanks for your constructive input :-)Faubourg
@Chev I hope that you'll also roll back your edit. I understand your frustration with questions that use the xcode tag but have nothing to do with Xcode specifically, but this isn't one of those.Grillwork
@Grillwork my comments may not actually be withdrawn after all. Where in that documentation is what you have quoted? I am reading through it now and I have found nothing as of yet to state that it realize on the xcode IDE, but if you can point me to it that would be fantastic, in fact reading the document it gives the impression that it is nothing to do with the IDE.Chev
@DavidKristensen After removing the target name and importing based on the Product name, things seem OK in my project; except for files that import the swift header and appear in a unittest. The -Swift headers appear to only be built for the current product, and thus a file that needs -Swift headers for both targets seems to have problems.Kee
@Chev Page 46, in the section titled Importing Swift into Objective-C.Grillwork
I did solve it with the help of gaige and the documentation. And I don't just throw the tag around: In this case, the header file SHOULD be created by and visible to XCode. It was not, thus yielding errors.Faubourg
@Chev There's nothing misstated -- I copied the quote directly from page 46. You've found a similar but different passage that says more or less the same thing.Grillwork
If you are stupid like me, it would be helpful to note that ProductModuleName is one thing, it is not Product and ModuleName concatenated together.Tompion
And if you are even stupider like me it would be helpful to note that you set the ProductModuleName in the Packaging section of the project settings (from here)Implication
F
172

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

Faubourg answered 5/6, 2014 at 15:17 Comment(23)
You should file a bug report about the spaces. Xcode should complain if you try to use spaces in a Swift module name. bugreport.apple.comHamnet
How do you check if Define Module has been set?Tallith
Define Module setting is under Build Settings > PackagingOverly
Make sure 'All' is selected (top left corner of the Build Settings viewport). You won't see the option with just 'Basic' selectedLinette
In Build Settings under Packaging, my Defines Module is set to Yes and I created a Product Module Name without spaces. The "*-Swift.h" is not generated by XCode 6. ???Staceestacey
The @objc directive seems to be optional, given that classes, methods and properties from a Swift class are visible to Objective-C without itSpode
One additional note: I first tried to set the mentioned properties on the target level, and it didn't work, the file "*-Swift.h" was not generated. It worked when I set it on project level.Sippet
I seemed to have followed these 2 steps and still seem to be having issues. I'll try starting a new project without the space in the name and see if i can get it to work.Renn
I'm also having the same issue. I have Defines Module set to Yes for both project and target, and a Product Module Name defined, with no spaces, but I can't seem to get this file generated.Lindyline
I myself can not get this to work either. It seems very buggy. I have it imported in my AppDelegate, and it works fine. I try to import it into another file and all hell breaks loose.Machute
Another note: There won't be an actual file added to your project. It is generated into the temporary build directory under "DerivedSources".Nagari
Just in case others are as confused as I was... I had to have blind luck here and simply import the header, as it wouldn't autocomplete.Exhibition
Even with Product Module name set, I had to replace spaces with an underscore.Thrips
Not only spaces but all non-alphanumeric characters cause problems. My product name contained a '.' and the solution was to replace the '.' with a '_' in the import. You don't have to replace the '.' in the product name! If your product name is "abc.def" you can write import "abc_def-Swift.h" without changing the product name. More information can be found here: developer.apple.com/library/ios/documentation/swift/conceptual/…Katiekatina
You cannot import -swift on header files (.h) only in the implementation.Susuable
If it`s still not working: Have you set your deployment target to > 7.0? This was my problem.Condemnation
As I've been reading that the <ProductModuleName>-Swift.h was auto-generated and I couldn't see it appearing at my project navigator, I made the dumb mistake to create a Header file with the same name and I was expecting things to work. So don't try to add your own <ProductModuleName>-Swift.h! Just import it in your .m files!Durwyn
excellent reply (with the comments above) but note I also had to Clean the Product before building againMarconigraph
Note that "ProductModuleName-Swift.h" will NOT actually show up in your project as Klaas mentioned. Type #import "ProductModuleName-Swift.h" (even though it will NOT autocomplete) and build(as sgawbwall said). In my case this works.Wolfhound
If none of the above mentioned steps are working for you. Check out macduff's answer below about importing the -Swift.h file in the obj-c .m file. I did this and it worked perfect!Counteroffensive
Important to note you will only find Defines Module setting under the Project Build Settings and not the Target Build settingsOutdoor
@Madhu in Xcode 8 this is working. You need to have "Defines Module" set to YES for project/target (seems like it's one setting) and you need to have the Product Module Name set for the target. At some point you will get an Objective-C Generated Interface Header Name which is what you should import (or change and import).Cathar
I set Defines Module YES for my target, but forgot to set Defines Module YES to my Product, Now I set YES for both and working.Fruin
L
129

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

Lovering answered 28/7, 2014 at 22:19 Comment(7)
This is correct, and... what if I need to use a Swift class in a objc header? I tried with forward declaration, it also doesn't work.Filmer
Whops, sorry, forward declaration works. I forgot to add a semicolon :PFilmer
For future searchers: in your headers do forward declaration by doing @class YourSwiftClass in the top of your .h files (instead of importing the umbrella header)Gaona
What if I need to use a Swift enum in a header file?Vivacity
I am not able to do #import “ProductModuleName-Swift.h”. My product name file is not created. How to create this?.Heulandite
Furthermore, if "#import ProductModuleName-Swift.h" is in any header file, then the ProductModuleName-Swift.h file will not be generated at all (as of Xcode 7.2) so if it had been deleted (e.g. by cleaning the build folder) then you will see error messages everywhere that the file is imported, even for .m files, adding to the confusion.Chanticleer
@Vivacity - You have to create a separate header file that defines the Swift type and import it into both your Swift and your Obj C code.Riyadh
K
63

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

Kingdom answered 2/10, 2014 at 1:19 Comment(5)
Thanks this is exactly what was holding me up. After fixing the swift build errors I was good to go. (Xcode Version 6.1.1)Indistinct
This is a great point and a good reason to turn on "continue building after errors" setting in Xcode.Obligor
That's exactly my case too: missing -Swift-h happened after same changes in swift source codeSeaquake
after seeing chicken/egg problem I realize my issue, Good analogyDistill
This problem was always returning to me (Xcode10) and I just interpreted Xcode as buggy. This was the answer thoughMerri
B
51

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)

Brendanbrenden answered 22/11, 2014 at 22:39 Comment(3)
This did it for me. I've been thinking that autocomplete will pick it up that I just assumed it wasn't there. Seeing in the directory cleared things up.Pilot
Sometimes seeing is believing. I wasn't able to get things working until following the recommended answer and then used these steps to find the generated header which, to my surprise, was postfixed with -Swift.h and not the name of my actual Swift file. Thank you!Medical
Thanks! Saved me a lot of timeFavored
G
44

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"

Gibbie answered 8/6, 2014 at 19:34 Comment(2)
If your project module name has hyphens in it, they should also be replaced with an underscore. So if your project name is "My-Project" use #import "My_Project.h"Lindly
Also, if your project module name starts with a number, it should also be replaced with an underscore. "1st Project" will be "_st_Project-Swift.h"Tribe
S
33

* 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 ...

Saltire answered 12/8, 2014 at 22:10 Comment(1)
That's true: no need for "Define Module" in Project or in TargetSeaquake
O
28

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>
Obligor answered 26/5, 2015 at 18:2 Comment(7)
In Framework I also note that in order to use Swift classes, they should be inherited from NSObject (even @objc will not help) and all methods/properties should be public.Evita
At least in current XCode 7 pre-release, until you inherit from NSObject they will not be available in generated swift header.Evita
100% did NOT fix my problem. EVERY solution is "make your classes public". I did this. Period. It still does NOT reveal the Swift classes in projects that use the framework.Jaret
@lespommes, you should write up a question and put up a sample project somewhere that exhibits the problem, someone could figure out what is going on... I just gave my answer as one possible solution, because it's the visibility mistake I make most frequently.Obligor
Oh yes, your hint with the import finally did the trick for me! Thanks a lot!Postscript
@Evita : I deleted my old response saying you didn't need to inherit from NSObject, you are right that if you do not do that (or declare the class @objc) ObjC will not be able to see or make use of the classes.Obligor
i spent two days bashing my head against the wall and the missing "public" declaration was the reason. THANK YOUEyeleen
A
14

I had the same problem. Seems like you have to adjust the settings (Defines Module and Product Module Name) before you add your first Swift file.

If you do it afterwards the "*-Swift.h" file will not be generated for this project even if you add further Swift files or delete the Swift file and create a new one.

Aniela answered 3/7, 2014 at 9:28 Comment(9)
I'm not sure that is true. I didn't decide to enable 'Defines Module' until well after creating many swift files and I was able to get a -Swift.h just fine.Liard
I'm actually sadden to report this is exactly the problem I had :( I had to delete my .swift and re-add it, and then all worked.Cutcherry
Now what? I am in middle of changing both Swift and ObjC code. My Project-Swift.h is not updating. I even tried to delete the header file.Indeterminable
I had to write this one up, dev.iachieved.it/iachievedit/?p=980. The tip on updating build settings prior to adding Swift files was key for me.Torsibility
Does this mean I'd have to recreate the project and re-add everything if I didn't do this? No matter what I do, I can't seem to get the *-Swift.h file to be generated.Cowell
Having the same problem... I already added a LOT of swift files to my project I don't want to readd everything!Filmer
I had the same problem, deleting and re adding my Swift files worked, but why :(Dispirited
Maybe this will help? The BridgingHeader are normally generated automatically when adding new files to the project. The Objective-C Bridgin Header seems also to be present in the build settings, when trying to call swift from obj-c https://mcmap.net/q/16306/-swift-to-objective-c-header-not-created-in-xcode-6Decibel
I'd like to add that it seems if you use version control (Git in my case) and switch to a branch where/when your project was Objective C only and then return to the ObjC/Swift app, the MyProject-Swift.h file is destroyed and will not be created again unless you remove all your Swift files, delete your Derived Data, and then re-add the Swift files. Very annoying.Writing
W
13

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.

Missing bridging header

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

Wendiwendie answered 3/11, 2015 at 13:40 Comment(2)
yep, for whatever reason my ObjC Swift.h file was generated with Swift class headers only after i've added empty bridging header file to the project!Complicated
Same here. My Swift code was not imported into generated *-Swift.h file until I added an empty Objective-C bridging header file.Marcelline
B
12

The file name is always preceded by your Target name. It is referred as Product name but practically it is the target name. So if you want it to build for a new target be ready to expect that_target-Swift.h file.

One way to handle this is

  1. Add a preprocessor for each of your target that is the name of your target itself (without spaces). Ex. MY_TARGET=1. Add this in Project settings->Build Settings->Preprocessor Macros for each of your targets.
  2. If you are using a PCH file,

Add these lines in the PCH file

#if MY_TARGET==1
#include "My_Target-Swift.h"
#elif THAT_TARGET==1
#include "That_Target-Swift.h" 
#endif

Advantage of using PCH file is that you don't have to include the headers everywhere.

  1. If you are not using a PCH file, just add these same lines in a single header and include that header wherever you need to use the swift classes.

This should work just fine.

Batfish answered 27/10, 2014 at 10:0 Comment(3)
I really like using this solution over importing the header directly into your source. I have a complicated project structure where the same source code is being used by multiple targets/projects. This avoids adding a messy #if #else statement all over my code. Thanks for the answer!Chine
This saved my day. I put the 5 lines of code in a "Something-Swift-Headers.h" file (with include in it). Then I import this new file from where I need it. I favor not putting this inside the PCH or any .h file to avoid cyclic dependencies and use the convenience header in .m files.Singularize
This is wrong on so many levels. Firstly, .pch files are heavily discouraged. Secondly, the file name contains the Product name and NOT the target name at all. It could be a coincidence that it's the same, but it used the Product name!Kulturkampf
K
12

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.

Kindle answered 4/6, 2015 at 15:31 Comment(1)
Is it possible to change/customise tool tip in IOS charts? if yes then can you give one example?Hornbeam
A
10

If Xcode is actually generating your -Swift.h header (deep inside DerivedData) but it doesn't refer to your Swift classes, make sure you also have a bridging header defined. The way I read the docs implied I only needed that for calling Objective-C from Swift, but it seems to be necessary for calling Swift from Objective-C too.

See my answer: https://mcmap.net/q/16490/-swift-to-objective-c-header-does-not-contain-swift-classes

EDIT: It is because of public vs. internal access modifiers, as I eventually found explained in the Apple docs:-

By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header.

Antihelix answered 15/1, 2015 at 21:9 Comment(2)
Yes, this really helped. I didn't knew that. I thought, that it is just needed for the Obj-C from Swift use-case.Decibel
Happy to help. I've edited the answer because I since found out the reason for this behaviour.Antihelix
I
8

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.

enter image description here

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.

Ingram answered 17/5, 2016 at 13:36 Comment(3)
Shouldn't it be #import?Kulturkampf
Let's say my project name is ABC, then "ABC-Swift.h", must exist in my project right? I can't find it. Do in need to manually create it? I am not creating any swift files my self, just including a cocoa pod which is written in swift.Consensual
The Bridging Header (in your case, ABC-Swift.h) isn't created automatically. When you try to create your first Swift file in an Obj-C project, Xcode normally prompts you to add one. You can also create a Bridging header on your own as shown here (control+f for 'bridging header'): developer.apple.com/library/content/documentation/Swift/…Ingram
P
6

This answer addresses the use-case where you may already have some Objective-C code that calls Swift classes and then you start receiving this error.

How To Fix Issue

The following steps ultimately resolved all of the issues for me. I read above someone mentioning the "chicken and the egg" and it is exactly that concept which led me to this procedure. This explicit process shows that one has to remove any Objective-C code referencing Swift classes until after the header is generated.

  1. Comment out the #import "ProductModuleName-Swift.h" statement in your Objective-C implementation file
  2. Comment out any references in the Objective-C implementation file to Swift Classes
  3. Clean & Build
  4. Resolve all errors/warnings
  5. Remove the comment on the #import "ProductModuleName-Swift.h" statement
  6. Clean & build (successfully or fix any remaining errors, verify that you are not referencing any Swift classes in Objective-C at this point. If so temporarily comment these out)
  7. Verify that "ProductModuleName-Swift.h" is generated by Cmd-Clicking on the class name of the #import "ProductModuleName-Swift.h" statement
  8. Remove the comment on the code referencing Swift classes in the Objective-C implementation file.
  9. Clean & Build as normal (the "ProductModuleName-Swift.h" should be generated and your Objective-C code referencing Swift Classes can be used as normal)

Nota Bene: The answers about changing spaces to underscores and the Defines Module to YES as given above still applies when performing this process, as do the rules specified in the Apple Documentation.

Bridging Header Path

In one error, the file ProductModuleName-Bridging-Header.h was not being found during the build process. This fact generated an error

< unknown>:0: error: bridging header '/Users/Shared/Working/abc/abc-Bridging-Header.h' does not exist

Closer inspection of the error indicated that the file would never exist at the location described because it was actually located at (a wrong path)

'/Users/Shared/Working/abc/abc/abc-Bridging-Header.h'. a quick search of the target/projects build settings to make the correction manually and the abc-Swift.h file was again auto generated.

build-settings

Prominent answered 4/6, 2015 at 3:6 Comment(2)
What if your project is so big that commenting out all references of swift code in objective c is impractical?Agma
You could also get errors when placing 'MyProject-Swift.h' within #ifdef FB_SONARKIT_ENABLED or #if DEBUG - then it might work in the simulator but not when building a release...Rosemarierosemary
B
6

Ok, here are all the things you really need!

1.Remove all the swift files you have added, and compile the code, without any errors.

----------

enter image description here

----------

2.Go to the "Projects" build settings, and set the product module name. Project must have a Product Module Name that does not include spaces.

----------

enter image description here

----------

3.Defines Module must be set to Yes in Build Settings, under Packaging, in your project, and not target!

----------

enter image description here

----------

4.Now create a swift file or a view controller, in file-> newFile->

----------

enter image description here

----------

It will ask to create a bridging-header, allow it to make one. If you have declined it once, you will have to manually add a -Bridging-Header.h

5.Add @objc in the controller, to tell the compiler that there is some swift file, which needs to be exposed to ObjectiveC

----------

enter image description here

----------

6.Build the project and import #import "-Swift.h" in any of the objectiveC controller, and it will work! You can Command-click on it to see the actual file!

----------

enter image description here

----------

Hope this helps!

Boracic answered 10/3, 2017 at 5:6 Comment(1)
>> You can Command-click on it to see the actual file! This is coolInwrap
H
5

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.

Heim answered 20/8, 2014 at 9:23 Comment(0)
C
3

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."

Chauncey answered 5/6, 2014 at 14:38 Comment(2)
But, when I do that, I get a build error, AppName-Swift.h does not exist. Whereas, AppName-Bridging-Header.h does exist.Faubourg
@DavidKristensen : Did you get solution of it?, as I have stuck with same issueKoby
P
2

An actual file in the project is not created ([ProductModuleName]-Swift.h). Cmd + Click on the import either generates it on-the-fly (and in-memory) so you can see how the linkage is done, or opens a file somewhere in some Xcode cache dir, but it's not in the project dir.

You need to set Defines Module project prop (in target's Build Settings) to Yes and if your module name has spaces or dashes - use _ in all imports of the [ProductModuleName]-Swift.h file.

You can import it in all .h and .m files where you use swift types or you can import it in the .pch.

So if my Module (project) is named "Test Project", I would import it like this, in the .pch file of my project (just there):

#import "Test_Project-Swift.h"
Peptone answered 29/10, 2014 at 19:17 Comment(0)
B
2

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

Blackwood answered 6/4, 2015 at 4:2 Comment(0)
W
2

Project must have a Module Name not including spaces. Defines Module must be set to Yes in Build Settings, under Packaging. commented out the #import statement:

If still you are having error in importing "ProductModuleName-Swift.h" then

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

Wendt answered 9/5, 2016 at 7:9 Comment(1)
My problem was that I had errors in my Swift code, too, but didn't know it until I uncommented the -Swift.h file. Thanks!Pilot
A
1

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.

Alfano answered 2/10, 2014 at 0:57 Comment(0)
S
1

I Found this solution

  • Create SwiftBridge.h
  • put #import “ProductModuleName-Swift.h”
  • Make this .h file public (important) Select the file -> In Show the file Inspector (right bar) -> Make it public

Now you can

#import "SwiftBridge.h"

instead of ProductModuleName-Swift.h

This's a workaround solution, for the next version of Xcode I think this problem will be solved. Good luck

Sparling answered 21/2, 2015 at 4:57 Comment(1)
Doesn't work. Just tried it. The line #import “ProductModuleName-Swift.h” just comes up with an error in the SwiftBridge.h file instead of the original Objective-C code file.Cowshed
S
1

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.

Selmaselman answered 30/6, 2015 at 13:13 Comment(0)
H
1

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.

Product Module Name in the build settings of target gives the correct module name which will work for your project

Homolographic answered 9/7, 2015 at 8:51 Comment(1)
If you can edit in a link to a screenshot I could edit your answer to display it for you.Hennessey
T
1

This may be an obvious point (maybe too obvious), but you must have at least one swift file in the project for the header to generate. If you are writing boilerplate or config code with the intention of writing swift later the import won't work.

Towrope answered 21/9, 2015 at 9:8 Comment(0)
P
1

In my case I had to set the deployment target to at least “OS X 10.9” and the -Swift.h header was automatically generated. Keep in mind that you can get a lot of deprecation warnings when you change the deployment target version, especially when you have an older and very large Objective C code base. In our case we also had a lot of work to do in XIB files & view classes.

Pilau answered 25/1, 2016 at 11:44 Comment(1)
For others - look in the Build Settings for your target, get the exact name "MyProject-Swift.h" - that's what you need to include as "#include "MyProject-Swift.h" in the appropriate Objc files. BTW, this answer really helped me - working to convert some older ObjectiveC open source.Carman
S
0

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

Stertorous answered 11/1, 2016 at 22:51 Comment(0)
S
0

I had similar problem but my project was compiling before and suddenly got error after few files code change. It took me while to figure out why I am getting 'File not found' error for myproject-swift.h file. The code changes I had done had some errors. Xcode did not point put those error instead all time showing the 'File not found error'. Then got copy of previous version code and I compared with new code and merged file one by one. After each file merge complied the project to find the error. So bottom line is if you have error in your code Xcode may just display 'file not found error' for myproject-swift.h file. Most likely you have compilation error in your project. Clean those error and it will work.

Savona answered 17/3, 2016 at 22:48 Comment(0)
H
0

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.

Hexone answered 13/4, 2017 at 2:29 Comment(0)
F
0

Sometimes you just need to unset and then set again the target membership on the obj-c .m file.

Foundation answered 3/11, 2017 at 16:8 Comment(0)
P
0

File was being generated but Xcode couldn't find it (I used Lou Zell's answer to prove that it was being generated).

This was my fix (Xcode 9.2) for a mixed ObjC/Swift framework:

  1. Find the expected generated file in DerivedData by searching in finder (TargetName-Swift.h)
  2. Drag it into your project in XCode and UNCHECK copy files.
  3. Click on the file in XCode and open the File Inspector (first tab on right panel).
  4. Change Location from Relative To Group to Relative To Build Products

The first two steps should allow you to build and use your project just fine, but the last two allow it to work on other computers (i.e. it still works when collaborating on a team project).

Pharos answered 27/2, 2018 at 23:10 Comment(1)
I am having the same problem with Xcode 10.2.1. I pull the Swift file into a large Objective-C project, it creates the bridging header. However when I try to import the "TargetName-MySwiftFile.h" it can't find it. I have followed the steps above and have the prefix @objc before the Swift class name.And anyone lend a hand? Also I have done a local search for "TargetName-MySwiftFile.h" and I find nothing. Thanks in advance.Malca
C
0

Sticking this here because I wasted days on this and didn't find a clear answer explaining all of this. I hope it helps save someone else some time.

I had a similar issue with Circle Jerk problem caused by refactoring Obj-C code into Swift. I was using the same class name for the Swift Class and taking the old class out of the target build. The issue arose because the ProjectName-Swift.h does not get built until the project builds.

The ONLY workaround I was able to come up with was to:

  1. NOT use the same name for the Swift Class as the old class being refactored into Swift.
  2. Have BOTH old class and new class in the project.
  3. Get it to build using old class.
  4. Then go back and change all the references to the old class name to new class name AFTER the update ProjectName-Swift.h was re-built with new class.

This was the ONLY way to I was able to transition over to the new class.

Consentaneous answered 6/7, 2023 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.