If you want to import objective-c class’s .h and .m file into the swift project, Xcode will create an Objective-C Bridging Header file in the swift project automatically. But sometimes you will find it does not create the objective-c bridging header file, so you need to create and configure it manually. This article will tell you how to do it.
1. Create Objective-C Bridging Header File Manually In Xcode Swift Project.
- Click File —> New —> File menu item at Xcode top menu bar.
- Then select the iOS Header File template in the popup dialog window and click the Next button.
- Input the header file name and select the swift project folder to save it. Please note the header file name format should be <Project Name>-Bridging-Header, for example, TestProject-Bridging-Header.h.
- Input below source code to import the Objective-C header file in the above Objective-C bridging header file.
#import "TestObjectiveCClass.h"
- Now the Objective-C Bridging Header file has been created manually successfully.
2. How To Configure Objective-C Bridging Header File In Xcode Target Build Settings.
- Click the Xcode swift project in the left project navigator pane.
- Then click TestProject in the TARGETS list in the center editor panel.
- Click the Build Settings tab at the top of the target detail pane.
- Then search Objective-C Bridging Header in the search box.
- Click the arrow before the Objective-C Bridging Header item to expand it. Then click the plus icon
(+)
to add the bridging header file name and path in the input text box. Please note the path is relative to the swift project.
- Now you can use the objective-c class in the swift class file directly, you can read the article How To Call Objective C Code From Swift Project to learn more.
Reference