Skip to content

Instantly share code, notes, and snippets.

@Vivaldi101
Created February 6, 2025 08:21
Show Gist options
  • Save Vivaldi101/a028aa677152859d2a990d87fdddafb1 to your computer and use it in GitHub Desktop.
Save Vivaldi101/a028aa677152859d2a990d87fdddafb1 to your computer and use it in GitHub Desktop.
iOS swift bridging
The Swift compiler/runtime knows about your Objective-C classes through the bridging header, but it requires that the bridging header be set up properly in your project build configuration. Here's how the process works:
1. Bridging Header Setup
When you create a bridging header, you are essentially telling the Swift compiler which Objective-C headers to include so that they become available in Swift. This process happens during the build phase and is part of your project's configuration.
2. How Swift "Sees" Objective-C Code
The bridging header tells the Swift compiler to automatically include the specified Objective-C files into the Swift compilation process.
When Swift sees a file with a #import (or #include in C++) directive in the bridging header, it knows to include that file for use in Swift code.
3. How it Works at Compile Time
During the build process, the Xcode or CMake build system (if using CMake) tells the Swift compiler to treat the bridging header as part of the compilation unit. This is done by setting an import path for Objective-C headers, ensuring that when the Swift code is compiled, the Objective-C classes and interfaces in the bridging header are made available to Swift.
4. The Role of the Bridging Header
The bridging header is typically included automatically in the build process. However, in Xcode or CMake, you need to set the proper path to the bridging header so that the compiler knows where to find it.
Once the bridging header is correctly linked to your project, the Swift compiler will treat Objective-C code as if it were part of Swift, and you can directly reference the Objective-C classes in Swift without needing to import the header files explicitly in every Swift file.
5. Swift Runtime and Objective-C
While the Swift compiler makes the Objective-C code available at compile time, the Swift runtime is responsible for the interaction between Objective-C and Swift at runtime. This is a seamless process where Swift and Objective-C objects can call methods and interact with each other due to the way they are bridged together in memory.
Step-by-Step Process:
Bridging Header: You define a bridging header (like YourProject-Bridging-Header.h).
Inside this header, you #import your Objective-C header files (e.g., AppDelegateFoo.h).
CMake or Xcode Build: During the build process, Xcode or CMake includes the bridging header for Swift.
Swift Compiler: The Swift compiler sees that the bridging header has been included and makes all of the Objective-C classes and methods available to your Swift code.
Swift Runtime: When the app runs, the Swift runtime ensures that calls from Swift to Objective-C code (and vice versa) are handled correctly.
To Summarize:
Swift knows about Objective-C classes because the bridging header tells the Swift compiler to include those files when compiling the Swift code.
The Swift compiler automatically picks up the Objective-C code through the bridging header during the build process.
After that, there is no need for explicit #import or #include in each Swift file — as long as the bridging header is set up and the files are properly linked, Swift will have access to the Objective-C classes and methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment