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

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

11 Answers11

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

Daniel Krom
  • 9,751
  • 3
  • 43
  • 44
  • 1
    Could you please explain the purpose of @objc prefix? – user842225 Aug 19 '15 at 15:10
  • btw, added step 1.5, will explain in a sec – Daniel Krom Aug 19 '15 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 Apr 07 '16 at 21:27
  • Maybe that's what's happening to me @Kevin_TA! Remove ALL? Yikes. – eric Apr 11 '16 at 19:08
  • If the swift class that you want to use inherit from a Objetive-C class such as NSObject or UIViewcontroller, put @objc prefix is not needed :D – MarMass Oct 05 '16 at 10:22
  • 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` – Daniel Krom Oct 05 '16 at 10:25
  • should we still use @Objc? – ravoorinandan Feb 27 '17 at 05: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>
Karol Kulesza
  • 909
  • 8
  • 10
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.

killerz
  • 99
  • 1
  • 2
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.

ThorstenC
  • 1,264
  • 11
  • 26
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

Genki
  • 3,055
  • 2
  • 29
  • 42
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 _.

Kaz Yoshikawa
  • 1,577
  • 1
  • 18
  • 26
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!

Travis Tubbs
  • 827
  • 1
  • 14
  • 32
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.

Igor Romcy
  • 336
  • 4
  • 7
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.

rsc
  • 10,348
  • 5
  • 39
  • 36
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.

Alfonso Tesauro
  • 1,730
  • 13
  • 21
-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

Locklight
  • 1
  • 1