Adding a Swift Bridging Header

 

What is a Swift Bridging Header?
A Swift bridging header allows you to communicate with your old Objective-C classes from your Swift classes.  You will need one if you plan to keep portions of your codebase in Objective-C.  It should be noted that even if you decide to convert all of your code to Swift, some classes or libraries you may use such as SVProgressHUD haven’t been rewritten in Swift and you will need to use a bridging header to use them.

 

Does Xcode automatically create one for me?
Yes.  Add a new Swift file to your Xcode project.  Name it as you please and you should get an alert box asking if you would like to create a bridging header.  Note: If you don’t receive a prompt to add a bridging header, you probably declined this message once before and you will have to add the header manually (see below).

Screenshot of Xcode Prompt

[adinserter block=”1″]

How to create a Swift Bridging Header Manually

  1. Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
  2. Name your file “YourProjectName-Bridging-Header.h”.  Example: In my app Station, the file is named “Station-Bridging-Header”.
  3. Create the file.
  4. Navigate to your project build settings and find the “Swift Compiler – Code Generation” section.  You may find it faster to type in “Swift Compiler” into the search box to narrow down the results.  Note: If you don’t have a “Swift Compiler – Code Generation” section, this means you probably don’t have any Swift classes added to your project yet.  Add a Swift file, then try again.
  5. Next to “Objective-C Bridging Header” you will need to add the name/path of your header file.  If your file resides in your project’s root folder simply put the name of the header file there.  Examples:  “ProjectName/ProjectName-Bridging-Header.h” or simply “ProjectName-Bridging-Header.h”.
  6. Open up your newly created bridging header and import your Objective-C classes using #import statements.  Any class listed in this file will be able to be accessed from your swift classes.

 

Swift Bridging Header

Add the header to your project build settings. (Step 5)

Import your Objective-C classes

Import your Objective-C classes (Step 6)