user842225
user842225

Reputation: 5979

import swift class in objective-c, <myModule>-Swift.h file not found

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

Upvotes: 45

Views: 51319

Answers (11)

Locklight
Locklight

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

Upvotes: -1

Alfonso Tesauro
Alfonso Tesauro

Reputation: 1792

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.

Upvotes: 0

rsc
rsc

Reputation: 10669

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.

Upvotes: 0

Igor Romcy
Igor Romcy

Reputation: 366

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.

Upvotes: 0

Karol Kulesza
Karol Kulesza

Reputation: 909

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>

Upvotes: 21

Daniel Krom
Daniel Krom

Reputation: 10060

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

Upvotes: 88

ThorstenC
ThorstenC

Reputation: 1304

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.

Upvotes: 5

Kaz Yoshikawa
Kaz Yoshikawa

Reputation: 1637

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

Upvotes: 1

killerz
killerz

Reputation: 99

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.

Upvotes: 9

Travis Tubbs
Travis Tubbs

Reputation: 877

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!

Upvotes: 0

Genki
Genki

Reputation: 3195

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

Upvotes: 3

Related Questions