Junior — Middle
How is Objective-C code integrated into a Swift project?
sobes.tech AI
Answer from AI
To connect Objective-C code in a Swift project, a so-called Bridging Header is used — a special header file into which Objective-C headers are imported.
Steps:
- Create a Bridging Header (usually Xcode suggests creating it automatically when adding an Objective-C file to a Swift project).
- In this file, add
#import "YourObjectiveCFile.h"for all necessary Objective-C headers. - After that, Swift code can directly use classes and methods from Objective-C.
Example of a Bridging Header:
#import "MyObjectiveCClass.h"
Now in Swift:
let obj = MyObjectiveCClass()
obj.someMethod()
Thus, the Bridging Header serves as a bridge between Objective-C and Swift in a single project.