The beauty of Swift is that you can mix both languages. Therefore in an existing Objective-C project, you can continue to work and update your app with new Swift code.
Apple doc explain how to mix-and-match, but isn’t clear.
Steps to Using Swift in Objective-C
Add your swift file.
(Optional) When prompted to add bridging header, select yes if you want to use Objective-C in Swift (the other way round).
In your project/target build settings, ensure:
- Defines Module set to Yes
- Module Name does not include spaces
In your Objective-C file, to use the Swift code,
#import "YourModuleName-Swift.h"
NOTE: The header YourModuleName-Swift.h
is NOT visible in your project. It is autogenerated by Xcode.
Your module name is the one found in your build settings.
Steps to Using Objective-C in Swift
The other way round is similar, except:
- The header name is
YourModuleName-Bridging-Header.h
- The header is visible in Xcode
- You don’t have to import the header in Swift
In this bridging header file, you have to manually add statements to import Objective-C header which you want to expose to Swift.