Swift "Bridging-Header.H" File Not Allowing Me to Instantiate Objective-C Classes in .Swift Files

Swift Bridging-Header.h file not allowing me to instantiate objective-c classes in .swift files

You need to add it to your target's build settings:

  1. In Xcode, if you go into the build settings for your target, and scroll all the way down you'll find a "Swift Compiler - Code Generation" section.

  2. Set "Objective-C Bridging Header" to <#PROJECT_NAME>-Bridging-Header.h

  3. I'm not sure of the correct value for "Install Objective-C Compatibility Header", but it's a yes/no, so you can toggle that if it doesn't work at first.

Trouble with Swift's Bridging-Header.h

You are missing an import. Add the following to your Objective-C Header:

#import <CoreData/CoreData.h>

Swift bridging header file won't work with use_frameworks

A) Create a Bridging Header file named
"ProjectName-Bridging-Header.h" in the root folder of your project.

B) Go to the project build settings and set the following values:

  • "Install objective-c compatibility header" : YES
  • "Objective-C Bridging Header" : path of your bridging header (e.g. "ProjectName/ProjectName-Bridging-Header.h"

After that you can use the header file to import all your ObjectiveC files which you want use within swift code.

NOTE: if required set the path as a recursive both in the resource headers and the Swift compiler search section.

Xcode not automatically creating bridging header?

The reason of your issue is, the Xcode build settings still holds the path to auto generated Bridging Header file. You cannot get any build errors because of the header file (ProjectName-Bridging-Header.h) still exist in your project directory.

How to resolve:

Click on your project target, Go to Build Settings tab (choose all instead of basic), search for Bridging Header. You can see the Xcode generated path entry. Select it and click on delete button.

Also, make sure to delete the Xcode auto generated bridging header file (ProjectName-Bridging-Header.h) from your Xcode project directory.

Now, try to import the Obj-C file once again. You can see the prompt to create a bridging header file as expected.

Sample Image

Importing Project-Swift.h into a Objective-C class...file not found

I was running into the same issue and couldn't get my project to import swift into obj-c classes. Using Xcode 6, (should work for Xcode 6+) and was able to do it in this way....

  1. Any class that you need to access in the .h file needs to be a forward declaration like this

@class MySwiftClass;


  1. In the .m file ONLY, if the code is in the same project (module) then you need to import it with

#import "ProductModuleName-Swift.h"

Link to the apple documentation about it

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c



Related Topics



Leave a reply



Submit