How to Call Objective-C Code from Swift: Friendship of Languages in iOS

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Objective-C and Swift: Two Worlds, One Project

\n\n

You write in Swift, but you have a library written in Objective-C, or old but proven code in ObjC. Sound familiar? Don't worry: Apple has provided an excellent mechanism for these two languages to work together. In this article, we'll break down how to call Objective-C code from Swift — quickly, simply, and without headaches.

\n\n

This question is one of the most popular on Stack Overflow (thousands of views and hundreds of votes). And for good reason: the ability to integrate Objective-C into Swift opens up access to a huge legacy of libraries and code written over decades of iOS development. Let's finally figure out how it works.

\n\n

What is a Bridging Header and Why Do You Need It?

\n\n

The secret to the friendship between Swift and Objective-C is a special file called the Bridging Header. It acts like a translator: it tells Swift which Objective-C classes and functions are available and how to call them correctly.

\n\n

When you create a bridging file, the compiler automatically picks it up and makes all declarations from Objective-C visible in Swift. You simply import the ObjC headers into this file — and you're done.

\n\n

How to Create a Bridging Header in Xcode

\n\n

The easiest way is to let Xcode do it for you. Simply add a new Objective-C file to your project (File → New → File → Objective-C File). Xcode will ask: "Would you like to configure an Objective-C bridging header?" — answer "Yes". After that, a file named YourProjectName-Bridging-Header.h will appear in your project.

\n\n

If you are already working on a project and want to add the bridge manually, do this:

\n\n
    \n
  • Create a new header file (File → New → File → Header File) and name it, for example, MyProject-Bridging-Header.h.
  • \n
  • In the project settings (Build Settings), find the Objective-C Bridging Header parameter and specify the path to your file.
  • \n
\n\n

Done. Now everything you write in this file will be available in Swift.

\n\n

Writing an Objective-C Class and Connecting it to Swift

\n\n

Let's create a simple class in Objective-C that will greet the user. First, the header file (Greeter.h):

\n\n
// Greeter.h\n#import <Foundation/Foundation.h>\n\n@interface Greeter : NSObject\n\n- (NSString *)greetWithName:(NSString *)name;\n\n@end\n
\n\n

And now, the implementation (Greeter.m):

\n\n
// Greeter.m\n#import "Greeter.h"\n\n@implementation Greeter\n\n- (NSString *)greetWithName:(NSString *)name {\n    return [NSString stringWithFormat:@"Hello, %@! Nice to meet you.", name];\n}\n\n@end\n
\n\n

Now, the most important part. Open your Bridging Header (e.g., MyProject-Bridging-Header.h) and add this:

\n\n
// MyProject-Bridging-Header.h\n#import "Greeter.h"\n
\n\n

That's it! Now you can use the Greeter class in Swift as if it were written in Swift:

\n\n
// ViewController.swift\nimport UIKit\n\nclass ViewController: UIViewController {\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \n        let greeter = Greeter()\n        let message = greeter.greet(withName: "Alex")\n        print(message) // Prints: "Hello, Alex! Nice to meet you."\n    }\n}\n
\n\n

Note: the method greetWithName: is transformed in Swift to greet(withName:). This is an automatic adaptation of method names from Objective-C to Swift style.

\n\n

Working with Objective-C Frameworks and Libraries

\n\n

Often you need to connect not your own code, but a ready-made library in Objective-C (for example, an old version of AFNetworking or CocoaLumberjack). This is even simpler:

\n\n
    \n
  • Install the library via CocoaPods, Carthage, or SPM.
  • \n
  • Import its headers into your Bridging Header.
  • \n
  • Use

Blogs

Book Recommendations