Search code examples
objective-cswiftxcodelanguage-interoperability

Importing Swift into Objective-C: Objective-C header "-Swift.h" is not created


In my Swift Project, I added a new cocoa Objective-C class (UIViewController), Xcode prompted me whether I want it to automatically generate an Objective-C bridging header file.

I am a bit confused because the generated file is ProductModuleName-Bridging-Header.h while, as per Apple documentation, I was waiting to get a ProductModuleName-Swift.h instead (In order to import Swift into Objective-C).

The ProductModuleName-Bridging-Header.h is mandatory in order to be able to import Objective-C from Swift, and not the inverse.

Any clarification on this?

Thanks


Solution

  • The behavior you describe is exactly correct. You're adding Obj-C to your Swift project, therefore Xcode offers to create a -Bridging-Header.h file which allows that imported Obj-C to be used in your Swift code. This is described in the documentation you linked under "Importing Objective-C into Swift".

    Unlike the bridging header, the -Swift.h file doesn't appear in your file hierachy, and is not something you see or edit. It's generated and managed entirely by Xcode during the build process. You simply import into .m files when needed.