Bridging Header file: Objective-C in Swift

Created By Hardik Trivedi  |  Last Updated on : 2018-12-01

Objective

The Bridging Header file is the ​main concept about use Objective-C programming code in Swift project. It's one type of bridge between Objective-C and Swift programming language.

The Bridging Header file is the main concept about use a Objective-C programming code in Swift project. It's one type of bridge between Objective-C and Swift programming language. You need to know that Apple provides us Bridging-Header file concept for it.

What is Bridging-Header file?

It's one type of file with extension .h. Apple provides one type of bridge file between Objective-C code & Swift code. Its main functionality is method or variable which is accessible in Objective-C should appear in Swift project.

Here is some step you need to follow creating Bridging-Header file. You can create this file at any stage of the project before importing any type of Objective-C file.

Let's consider you have one Swift project. So, please create one Swift project. If you don't know about how to create it please check our previous blog: 

Introduction of Xcode - iOS Development

Now, we have one Swift project and you can write one simple method in Swift which is just print some statement. So, follow below code and put it in a ViewController.swift file:

func functionInSwiftProject()
{
     print("Swift Project Function")
}

This is the simple swift method; now called it from a viewDidLoad()method:

override func viewDidLoad()
{
     super.viewDidLoad()
     // Do any additional setup after loading the view, typically from a nib.
          
     self.functionInSwiftProject()
}

Now we are going to create Bridging-Header file for accessing some Objective-C code. So, please follow below steps:

Create Objective-C file in Swift:

Now, the first step is to create Objective-C file by right-click on Project-Name on left side panel (Project Navigation bar). If you have any difficulty to find it please check below photo:

Choose Template iOS

Now choose Cocoa Touch Class & click Next button.

Choose Objective-C Langauge:

Now, enter class name whatever you want; I gave here "ObjectiveCFile" with subclass of NSObject and choose lanague Objective-C. Check followign photo for refereance:

New File options

Now, click on Next button.

Bridging-Header popup:

You will see the following popup in screen. Please click "Create Bridging Header" button which creates file in Swift project.

Bridging-Header Popup

If you don't see this dialouge. Don't worry about it! You guys need to set manual path for your Bridging-Header file via Target -> Project Name -> Build Settings -> Objective-C Bridging Header. Create "PROJECTNAME-Bridging-Header.h" file & give path here and check below referance photo.

Build Settings Manual Path

Thoes who has Bridging-Header popup; Xcode will automatic generate .h file and set path in Build Settings. You can check via above path. it's automatically set but, if not than you have to make manually set there.

Bridging-Header file in Project Navigation:

Now check you created or automatic created Bridging-Header file in your Project naviation panel with your NSObject sub-class created file. Check below image for referance:

Project Navigation Panel

Now, we have Objective-C file in Swift project and in addition we have now Bridging-Header file.

Some coding stuff in Objective-C & Swift both file:

Now, i created one method name functionInObjectiveCProject in .m file for print some stuff in LOG. Check below code snippest:

//
//  ObjectiveCFile.m
//  SwiftProject
//
//  Created by Hardik Trivedi on 26/11/18.
//  Copyright © 2018 iHardikTrivedi. All rights reserved.
//

#import "ObjectiveCFile.h"

@implementation ObjectiveCFile

- (void) functionInObjectiveCProject
{
    NSLog(@"Objective-C Project Function");
}

@end

And we call this method from Swift class so we make sure it's public method. So, my .h file looks like:

//
//  ObjectiveCFile.h
//  SwiftProject
//
//  Created by Hardik Trivedi on 26/11/18.
//  Copyright © 2018 iHardikTrivedi. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ObjectiveCFile : NSObject

- (void) functionInObjectiveCProject;

@end

NS_ASSUME_NONNULL_END

Now, we want this method use in Swift project. So, we have to give or implement this header file in Bridging-Header.h file. For me; i have to implement "ObjectiveCFile.h" file in SwiftProject-Bridging-Header.h file. Check below snippest:

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "ObjectiveCFile.h"

Access Objective-C method in Swift file:

Now, we go back to our file ViewController.swift file and create object of Objective-C file. it's accessible and you can detect your Objective-C file in Swift file. Now, your ViewController.swift file looks like below snippest:

//
//  ViewController.swift
//  SwiftProject
//
//  Created by Hardik Trivedi on 26/11/18.
//  Copyright © 2018 iHardikTrivedi. All rights reserved.
//
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.functionInSwiftProject()
        
        let objOfFile: ObjectiveCFile = ObjectiveCFile()
        objOfFile.functionInObjectiveCProject()
    }

    func functionInSwiftProject()
    {
        print("Swift Project Function")
    }
    
}

Final Output:

Now build and run project. You can build with (Cmd + B) and run with (Cmd + R). See your output in below panel which is called LOG part.

Log Panel

Yeah!, It's Swift Function & Objective-C Function both called from Swift project. Now you can access any Objective-C code with Bridging-Header.h file.

 

 

Summary

iOS Developer can access Objective-C code in Swift project using "Bridging-Header.h" file concept. You can easily access Objective-C code in Swift project.

If you have any query regarding this blog comment below. If you have any idea or query about iOS Applicaiton Developement / Apple Watch Application / iMessage Extension / iOS Widgets Contact Uswe have expert iOS people in our company.

Created on : 2018-11-26
Written by

Hardik Trivedi

iOS Application Developer

Tags
Objective-C
Swift
Bridging-Header