45

I have an iOS project written with Objective-C. I created an Swift class in the project, the bridging header file for accessing objective-c in Swift is generated successfully, and it works fine.

My problem is the other way around. I want to import Swift class in objective-c code.

In xcode, target -> Build Settings--> Swift Compiler section, I see Objective-C Generated Interface Header Name field with value myModule-Swift.h , but when I import this header in my objective-c class:

#import "myModule-Swift.h"

I get compiler error:

myModule-Swift.h file not found

and in project, I cannot find this file either. How can I solve this problem?

My xcode version is 6.4

1
  • What do you mean classes? I don't show any class. myModule-Swift.h is the header xcode is supposed to generate.
    – user842225
    Commented Aug 19, 2015 at 14:52

11 Answers 11

88

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)

enter image description here

7
  • 1
    Could you please explain the purpose of @objc prefix?
    – user842225
    Commented Aug 19, 2015 at 15:10
  • btw, added step 1.5, will explain in a sec Commented Aug 19, 2015 at 15:12
  • 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.
    – Kevin_TA
    Commented Apr 7, 2016 at 21:27
  • Maybe that's what's happening to me @Kevin_TA! Remove ALL? Yikes.
    – eric
    Commented Apr 11, 2016 at 19:08
  • 2
    @MarMass Yes , this answer is not up to date, before swift 2.0 you had to use @objc or inherit from NSObject (btw UIViewController also inherits from NSObject), now for @objc you must inherit from NSObject Commented Oct 5, 2016 at 10:25
21

In case anybody is wondering why after spending hours on this issue it is still unresolved, you might be in a situation similar to mine, where I was actually developing a framework, rather than an app.

In such case, the format of the import statement for Objective-C Generated Interface Header is as follows:


#import <ModuleName/ModuleName-Swift.h>
3
  • 1
    Thank you for this! Commented Jul 17, 2018 at 8:12
  • Not working for me. All the more aggravating because I see other examples of that within the same ****ing repo that use this syntax, have the same target, etc. So ****ing aggravating.
    – Adrian
    Commented Mar 4, 2020 at 0:33
  • 1
    Thanks. This saved me a tonne of time Commented Sep 30, 2020 at 11:43
9

OMG.. the actual import statement was not "class-Swift.h" but rather "projectname-Swift.h"

You can find the name of the file if you look under build settings->Swift Compiler Code Generation -> Objective-C Generated Interface Header Name

The file was not generated when I dragged in Swift source into the GUI. Only when I right-clicked->Add file to "project". It then asked to generate the header files.

2
  • just importing "ourprojectname-Swift.h" in objective c file worked
    – Nagarjun
    Commented Oct 27, 2016 at 15:11
  • You can see what it should be under your TARGET build settings -> Product Module Name. It may not automatically be the name of the project / target Commented Feb 27, 2017 at 2:42
5

My addition to Daniel Kroms answer:

  • Never add -Swift.h Header to header. Even if it seems to work. Add the Import to .m file only!

  • In case you use in your header swift classes, make a forward declaration with @class swiftclassname before your @interface

Then you will see your real errors in your code.

2
  • 1
    what if I need to use a swift enum in a header file?
    – AlexW
    Commented Sep 14, 2017 at 16:42
  • @AlexW you have to move it to .m Commented Nov 28, 2017 at 9:46
3

For me, the problem was that I had bitcode on. When I clicked on the "Update to recommended project settings", it changed a few settings which probably the culprit. I turned "Enabled Bitcode" to "No" in the Build Settings and it is fixed now.

enter image description here

1

I was stacked this for a quite a while. In my case, my target name is something like "my-app" using dash as a part of target name. I tried to #import "my-app-Swift.h", but Xcode kept giving me errors.

I dug under 'DerivedData' folder and I found "my_app-Swift.h". So if you are using some interesting characters for the target name. You may try replace those with underscore _.

0

I ran into the same issue yesterday and worked for hours to fix it with no avail. Others may have been in the same boat as I. I did all of the steps described above, but nothing worked.

The cause of mine breaking was because of project name artifacts all over my project (from previously changing its name improperly).

If all of the above steps fail, I would suggest doing like I did and renaming your project so that Xcode can reset somethings... That solved the problem for me. Doing so worked like a charm!

0

In my case, I have forgotten to check a swift framework to the target/classes that I was using it, really specifically case but it may help someone in the future.

0

For me the solution was to create a new target. For an unknown reason, the target that I had didn't have that "Swift Compiler - General" settings and thus no to "Objective-C Generated Interface Header Name" field. Having that field specified in the project was not enough.

0

This is not an exact answer but more of a workaround, but can save you time in some difficult cases. If you, as suggested by some of the the previous answers, can actually find the swift header buried inside the derived data folder, you are allowed to import it using the full path. This is not specific to the swift header, instead it can be applied to any header. Hope you can find this answer useful.

-1

This answer solves my problem, but what's confusing to me is that when I convert bitcode to YES, then clean Xcode DerivedData, bulid again,also Bulid succeeds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.