It happens to the best of us: you find yourself needing to use an older Objective-C library and you’ve forgotten how to add the bridging header to your Swift project so that you can import the library. No worries, it’s four simple steps:
- Select your project. Add a header file to your project under File > New File (command+n) and select
Header File. Name this file [MyProjectName]-Bridging-Header.h and save it to your main project folder. This will be the single header file where you import any Objective-C code you want your Swift code to have access to.
- On the search bar, type in
- Double click the
Objective-C Bridging Headerrow and type in the path and name of your file (relative to your project) which is probably something like MyProject/MyProject-Bridging-Header.h or simply MyProject-Bridging-Header.h if the file lives in the project root folder
You only need one Bridging Header. Add your #import statements to this file, and your classes will now be available in your Swift code without any extra import statements!