Objective-C
Swift
Programming
iOS development
Code Integration

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.

Browse interview questions

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.

  1. 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.
  2. 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:

objective-c
1// Person.h
2#import <Foundation/Foundation.h>
3
4@interface Person : NSObject
5
6@property (nonatomic, strong) NSString *name;
7@property (nonatomic, assign) NSInteger age;
8
9- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;
10- (NSString *)greeting;
11
12@end
objective-c
1// Person.m
2#import "Person.h"
3
4@implementation Person
5
6- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {
7    self = [super init];
8    if (self) {
9        _name = name;
10        _age = age;
11    }
12    return self;
13}
14
15- (NSString *)greeting {
16    return [NSString stringWithFormat:@"Hello, my name is %@ and I am %ld years old.", _name, (long)_age];
17}
18
19@end

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:

swift
let person = Person(name: "John", age: 30)
print(person.greeting())

Common Issues and Solutions

IssueSolution
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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.