How do I call Objective-C code from Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Swift and Objective-C are both powerful programming languages used by developers in the Apple ecosystem. Swift is known for its safety features and modern syntax, while Objective-C is robust and has access to a vast library of existing code and frameworks. Merging these two languages in one project can be incredibly powerful. Here’s how you can call Objective-C code from Swift, complete with necessary configurations and examples.
Configuring Objective-C bridges in Swift
Before you can use Objective-C code in a Swift project, you need to ensure your project is properly configured to bridge the two languages.
- Create Objective-C classes in Swift project:
- If your Swift project does not already contain Objective-C code, you will need to add an Objective-C file (
File -> New -> File -> Objective-C File). - Upon adding your first Objective-C file, Xcode will prompt you to create a bridging header. Accept this when prompted to allow Swift to interact with your Objective-C files.
- Bridging Header:
- The Bridging Header file is crucial for using Objective-C code in Swift. It is where you import any Objective-C headers of classes that you need to use with your Swift code.
- If by chance the prompt does not appear for creating the bridging header, you can add it manually:
- Add a new header file to your project (
File -> New -> File -> Header File). - Name it
<YourProjectName>-Bridging-Header.h. - Go to your project settings, select your target, and under Build Settings, look for
Objective-C Bridging Header. Enter the path of your bridging header relative to your project's root.
Calling Objective-C Code from Swift
Once the bridging setup is correctly configured, you can begin using Objective-C classes within Swift.
Example 1: Using an Objective-C Model in Swift
Suppose you have an Objective-C class Person:
To use this in Swift, add #import "Person.h" to your Bridging Header. Then, in Swift, you can instantiate and use the Person class like so:
Common Issues and Solutions
| Issue | Solution |
| Swift can't see Objective-C classes. | Ensure the classes are imported in the Bridging Header. |
| Compilation Errors in Bridging Header. | Ensure framework imports in Objective-C headers are correct and necessary frameworks are linked in the project settings. |
| Runtime crashes involving Objective-C code. | Debug usual suspects like nil pointers, memory leaks, and unintended null returns. |
Calling Swift from Objective-C
For completeness, while the focus here is calling Objective-C from Swift, the reverse is also common and requires the use of @objc annotations in Swift to expose Swift classes to Objective-C.
Best Practices
- Keep data types compatible. Objective-C uses reference types for objects while Swift uses value and reference types.
- Handle nullable and non-nullable annotations properly to avoid runtime crashes.
- Use modern Objective-C syntax and conventions to ensure better compatibility and readability.
Calling Objective-C from Swift allows developers to harness the robustness of Objective-C while enjoying the syntactic sugar and safety of Swift, making it an essential skill for seasoned iOS/macOS developers.
Related reading
- How do I call Objective-C code from Swift?
- How do I center text horizontally and vertically in a TextView?
- How do I change screen orientation in the Android emulator?
- How do I change the android actionbar title and icon
- How do I change the background color of a SwiftUI View?
- How do I change the background color of the ActionBar of an ActionBarActivity using XML?
- How do I change the color of the text in a UIPickerView under iOS 7?
- How do I change the font size of a UILabel in Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.