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.
Introduction to Mixing Swift and Objective-C
Swift, introduced by Apple in 2014, was designed to coexist seamlessly with Objective-C, allowing developers to utilize existing codebases and libraries. This interoperation is fundamental for maintaining and expanding older iOS and macOS projects, ensuring that you can leverage existing components while incorporating Swift's modern syntax and features.
Prerequisites
Before we delve into the details of calling Objective-C code from Swift, ensure you have:
- A mixed-language project set up, with both Swift and Objective-C files.
- Objective-C files with
.mand Swift files with.swiftextensions. - Bridging Header configured correctly for seamless integration.
Bridging Header
The bridging header is crucial for calling Objective-C code from Swift. It's essentially a header file within your project that exposes Objective-C classes to Swift files.
Setting Up a Bridging Header
- Create a Bridging Header: If Xcode prompts you to create a bridging header when adding an Objective-C file to a Swift project, accept the offer. If not:
- Create a new header file and name it something like
ProjectName-Bridging-Header.h. - In your bridging header, import the Objective-C headers you want to expose to Swift.
- Configure the Bridging Header Path:
- Go to your project settings.
- Navigate to
Build Settings. - Find the
Objective-C Bridging Headersetting and input the relative path to your bridging header file, such asProjectName/ProjectName-Bridging-Header.h.
Accessing Objective-C Code in Swift
With the bridging header configured, Swift can utilize the Objective-C classes and methods as if they were native Swift code.
Example: Calling an Objective-C class in Swift
Suppose you have an Objective-C class Greeter:
Objective-C Implementation (Greeter.m):
Objective-C Header (Greeter.h):
Using the Greeter class in Swift:
Data Type Compatibility
Swift and Objective-C interoperable projects require a correct understanding of type compatibility.
- Classes/AnyObject: Swift classes and Objective-C classes (
NSObject) are interoperable. - Primitives: Swift's
IntandBoolmap to Objective-C'sNSIntegerandBOOL. - Collections: Swift's
ArrayandDictionaryare bridged toNSArrayandNSDictionary, respectively.
Here's a table summarizing some key type mappings:
| Swift Type | Objective-C Type |
Int | NSInteger |
Bool | BOOL |
String | NSString |
Array | NSArray |
Dictionary | NSDictionary |
Optional | nullable/ null-resettable |
AnyObject | id |
Handling Nullability
In Swift, optionals (e.g., String?) signify the potential absence of a value, which aligns with Objective-C's convention of using nil. However, you must explicitly declare nullability in Objective-C headers to avoid miscommunications.
- Non-null (
nonnull): Maps to non-optional types in Swift. - Nullable (
nullable): Maps to optional types in Swift.
Objective-C to Swift Conversions
To utilize Swift from Objective-C codebases, doors open through an @objc attribute. This attribute exposes Swift functions and classes to Objective-C.
Exposing Swift to Objective-C
You would then use this Swift class in Objective-C as follows:
Conclusion
Calling Objective-C code from Swift enriches your development process, fostering code reuse, and adding flexibility. While configuring a bridging header and understanding type compatibility is essential, mastering these concepts allows you to transition smoothly between Swift and Objective-C, ensuring robust implementation for your applications.
Using these practices, you can evolve your apps by utilizing existing Objective-C libraries while embracing Swift's modern programming paradigms.
Related reading
- 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?
- 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.