Objective-C and Swift: Two Worlds, One Project
\n\nYou 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\nThis 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\nWhat is a Bridging Header and Why Do You Need It?
\n\nThe 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\nWhen 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\nHow to Create a Bridging Header in Xcode
\n\nThe 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.
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
Done. Now everything you write in this file will be available in Swift.
\n\nWriting an Objective-C Class and Connecting it to Swift
\n\nLet's create a simple class in Objective-C that will greet the user. First, the header file (Greeter.h):
// Greeter.h\n#import <Foundation/Foundation.h>\n\n@interface Greeter : NSObject\n\n- (NSString *)greetWithName:(NSString *)name;\n\n@end\n\n\nAnd now, the implementation (Greeter.m):
// 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\nNow, the most important part. Open your Bridging Header (e.g., MyProject-Bridging-Header.h) and add this:
// MyProject-Bridging-Header.h\n#import "Greeter.h"\n\n\nThat's it! Now you can use the Greeter class in Swift as if it were written in Swift:
// 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\nNote: the method greetWithName: is transformed in Swift to greet(withName:). This is an automatic adaptation of method names from Objective-C to Swift style.
Working with Objective-C Frameworks and Libraries
\n\nOften 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