If you decided to start using Swift in your existing Xcode project, it can become a pain if your project is large and contains multiple targets. Follow steps below to be able to use Swift classes in your Objective-C code:
- Create a new
*.swift
file in your Objective-C project, or drag a .swift file with Finder - Xcode prompts you to create an
Objective-C bridging header
file. Confirm it or add the bridging header manually - Use
@objc
attribute for your Swift class:import Foundation @objc class YourController: UIViewController { }
- In Build Settings of your project check parameters below:
Defines Module : YES
Install Objective-C Compatibility Header : YES => Once you’ve added *.swift file to the project this property will appear in Build Settings
Objective-C Generated Interface Header : $(PROJECT_NAME)-Swift.h - Import Swift interface header in your *.m file like this:
#import "YourProject-Swift.h" // $(PROJECT_NAME)-Swift.h