Absolutely! To import an Objective-C code into a Swift Xcode project you will need to create a bridging header (this allows you to communicate with your old Objective-C classes from your Swift classes).
The process of doing this is relatively painless. Go to File -> New -> File… , a window will appear in which you will select “Objective-C File”, name the file however you choose, then select Create. A pop-up will appear asking you if you want to create a bridging header. Choose “Create Bridging Header” and should be all set. To complete the process, delete the .m file that you choose the name and move the bridging header to a more suitable group inside the project navigator.
Alternatively, you can manually create an Objective-C bridging header in Xcode by performing the following:
- Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
- Name your file “YourProjectName-Bridging-Header.h”. Example: In my app Station, the file is named “Station-Bridging-Header”.
- Create the file.
- 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.
- 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”.
- 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.
Please sign in to leave a comment.