How To Call Objective C Code From Swift Project

When you develop iOS app use swift, if you have some older function wrote by objective-c language, you can use Objective-C bridging header file to import your objective-c header files to the swift project. And then you can invoke the objective-c class function from your app’s swift code. This article will tell you how to do it with an example.

1. How To Add Objective-C Headers File Into Swift Project Steps.

  1. First you should create a Xcode swift project. You can read article How To Create A Swift Project In Xcode , How To Create Xcode Workspace And Add New / Exist Project Into It if you do not know.
  2. If you have old Objective-C file, you should not copy it into the project directly. You had better create a new Objective-C class file and then copy your old Objective-C .h and .m file’s code content into the newly created file accordingly follow below steps.
  3. Click File —> New —> File menu item at Xcode top menu bar.
    xcode file new file menu item
  4. Then select Cocoa Touch Class template in the iOS tab. This will create both the objective-c .h and .m files, if you want to create the objective-c .h and .m file separately, you can select Objective-C File and Header File template separately. Click Next button.
    xcode ios cocoa touch class template
  5. Input the cocoa touch class name in the Class input text box in next wizard dialog window, and select Objective-C coding language in the Language drop down list. Click Next button.
    input cocoa touch class name and select objective-c coding language
  6. Select the exist swift project folder in the next dialog, check the checkbox before the swift project in the Targets list, then click Create button to create the cocoa touch class.
    select the cocoa touch class saved swift xcode project folder
  7. If this is the first time you create Objective-C class file in swift project, it will popup a dialog to ask you whether you need to configure an Objective-C Bridging Header file or not, click the Create Bridging Header button in the dialog.
    xcode create bridging header popup dialog
  8. If you do not see above dialog, that means you had create objective-c class file before, and it will not create the objective-c bridging header file for you automatically. I will tell you how to add the Objective-C bridging header file and configure it manually in another article.
  9. Now you can see the objective-c .h and .m file ( TestObjectiveCClass.h & TestObjectiveCClass.m ) is listed at Xcode left navigator panel like below. It will also create an Objective-C Bridging Header file in the swift project, the file name is <Project Name>-Bridging-Header.h ( TestProject-Bridging-Header.h )
    xcode objective c bridging header file
  10. Now add below code in TestProject-Bridging-Header.h file to import the objective-c class header file.
    #import "TestObjectiveCClass.h"
  11. Now when you click TestProject in Xcode left navigator pane, then click TestProject in the editor TARGETS list,  then click Build Settings tab at the top of target detail panel and search Objective-C Bridging Header in the panel search box, you can see Xcode has already add the Objective-C Bridging Header file path and name in the related item.
    input objective c bridging header file path in xcode swift project - target - build settings - objective c bridging header
READ :   How To Add Resource Files Into Xcode Project And iOS App

2. How To Invoke Objective C Class In Swift Source Code.

Now add below objective-c source code in TestObjectiveCClass.h and TestObjectiveCClass.m file.

TestObjectiveCClass.h

//
//  TestObjectiveCClass.h
//  TestProject
//
//  Created by song zhao on 8/7/19.
//  Copyright © 2019 dev2qa.com. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface TestObjectiveCClass : NSObject

// Declare a static class method.
+(void)printHello;

// Declare an instance method.
-(NSString *)sayHello;

@end

NS_ASSUME_NONNULL_END

TestObjectiveCClass.m

//
//  TestObjectiveCClass.m
//  TestProject
//
//  Created by song zhao on 8/7/19.
//  Copyright © 2019 dev2qa.com. All rights reserved.
//

#import "TestObjectiveCClass.h"

@implementation TestObjectiveCClass


+(void)printHello{

    // Print log message.
    NSLog(@"Hello from objective-c class.");
}

// This function will return a NSString object with string messages.
-(NSString *)sayHello{
    NSString * var;
    
    var = @"Hello from objective-c class.";
    
    return var;
    
}

@end

Add below source code in swift project ViewController.swift file.

ViewController.swift

//
//  ViewController.swift
//  TestProject
//
//  Created by song zhao on 8/6/19.
//  Copyright © 2019 dev2qa.com. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var message:String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Call objective-c class method directly.
        TestObjectiveCClass.printHello()
        
        // Create an instance of objective-c class.
        let testObject:TestObjectiveCClass = TestObjectiveCClass()
        
        // Invoke the objective-c class's instance method.
        message = testObject.sayHello()

        // Print the message in Xcode debug console. 
        print(message)
    
    }
   
}

When you run the example, you can see the below output in Xcode debug area activate console.

2019-08-07 22:27:53.410723+0800 TestProject[3082:126700] Hello from objective-c class.
Hello from objective-c class.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.