Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 783
user842225 Avatar asked Aug 19 '15 14:08

user842225


People also ask

How do I import a Swift file into Objective-C class?

Import Swift code into Objective-C within the same framework: Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .

What is Swift H file in XCode?

${Your project name}-Swift. h is for objc based project to use swift code I remember. It is generated by XCode, to translate swift code to a header file for objc to import. If you don't need to use swift in objc project, you don't need it. As you can see you are simply a swift project, everything is swift.


2 Answers

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

like image 144
Daniel Krom Avatar answered Sep 18 '22 11:09

Daniel Krom


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> 
like image 35
Karol Kulesza Avatar answered Sep 19 '22 11:09

Karol Kulesza