Martin
Martin

Reputation: 768

How to force `Xcode-generated header` ... in a Swift Project (Call Swift from Obj-C)

Xcode does not generate this -Swift.h header file when the project is a Swift project. The documentation says:

When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h".

If I try to include the -Swift.h file anyway, it says file not found. I've been able to generate this file before in an Objective-C project. But I want to call Swift from Objective-C, in a Swift project.

How do I call Swift from Obj-C in a Swift project?

Thanks!

Upvotes: 1

Views: 2649

Answers (1)

Anatoli P
Anatoli P

Reputation: 4891

If the Objective-C file in which you import *-Swift.h is in the same Swift target that contains the Swift code you are trying to use in Objective-C, you should have no trouble importing it. To make sure you are using the correct name, check the Objective-C Generated Interface Header Name under Build Settings. If the target is a framework, you may need to import the -Swift.h header a little differently:

#import "YourFrameworkModuleName/YourFrameworkModuleName-Swift.h"

whereas in an application target you would just do

#import "YourAppModuleName-Swift.h"

This is something I found from my experience.

Upvotes: 7

Related Questions