News Maffat

We Promote Almost Everything…

img
Home / Technology Updates / Objective C file used in Swift Project – How to Make App in iOS – Steps

Objective C file used in Swift Project – How to Make App in iOS – Steps

/
/
/

This tutorial teaches you how to make iPhone app in Swift Programming Language and how to use Objective-C file with the Swift Project. You can make the Objective-C file and also import from the any folder to your Swift Project. The following steps may be easier to make than you think. Follow the Step 1 below to get started and go through for creating application and successfully run it.

Step 1 of 7: Install and Setting Up Your Development Environment

Download and install Xcode. Xcode 6.0 Beta is an Integrated Development Environment (IDE) to create all iPhone apps with Swift Programming Language but it requires OS X 10.9.3 or later to be installed. It contains a suite for software development tools for iOS application. After Installation Xcode; open Xcode.

Step 2 of 7: Create Swift Project in XCode

Create project with name ObjectiveCWithSwiftDemo or any other name and make sure that your project language must choose Swift which is shown in below screenshot.

Swift Project
Swift Project
Bridging-Header file
Bridging-Header file

Step 3 of 7: How to run your project

Now select Simulator (Top Left corner) from the drop down, which is near Run  button and select run or shortcut Command + B (for Build) and Command + R (for Run).

Step 4 of 7: Create Objective-C (.h & .m file) file in your project

Now, Create one Objective-C file that you want to use in Swift Project. For these follow below steps :

(1) Right click on Project and Click New File:

(2) Select Objective-C file and Click Next:

(3) Give that File name “ObjectiveCFile” and Create it:

(4) After Clicking Create button XCode ask for configure an Objective-C bridging header. You must say YES. This dialogue box shown only once when first Objective-C File create in Swift Project.

 

(5) Now, your project Navigation have two files looks like below:

bridging file
Bridging file in Swift Project

(6) Here, ObjectiveCFile.m is implementation file for Objective-C file execution but ObjectiveCFile.h header file must exists for both .m & .h file execute in Objective-C. So, now make header file: Right click on Project and Click new file & Choose Header file and give same name as ObjectiveCFile.m file name and created it . . .

Swift Navigation
Swift Navigation

Step 5 of 7: Code for .m & .h file which is blank.

 Now, Write a Code below the comment section in ObjectiveCFile.h header file,

- (void) displayMessgeFromCreatedObjectiveCFile;

which looks like:

#import <UIKit/UIKit.h>
	
@interface ObjectiveCFile : UIViewController
	
- (void)displayMessageFromCreatedObjectiveCFile;
	
@end

Here, create one method “displayMessageFromCreateObjectiveCFile” which have return type void.

Now, these header file import in implementation file and make some stuffs for method which is implemented in ObjectiveCFile.m and write code :

- (void) displayMessageFromCreatedObjectiveCFile
{
     NSLog(@”Hello. . . ”);
     NSLog(@”This Message From Created Objective-C File with Swift Project”);
}

So, your implementation file ObjectiveCFile.m looks like:

//
// ObjectiveCFile.m
// ObjectiveCWithSwiftDemo
//

#import <Foundation/Foundation.h>
#import "ObjectiveCFile.h"

@interface ObjectiveCFile ()
@end

@implementation ObjectiveCFile

- (void) viewDidLoad
{
     [super viewDidLoad];
}

- (void) displayMessageFromCreatedObjectiveCFile
{
     NSLog(@"Hello. . . ");
     NSLog(@"This Message From Created Objective-C File with Swift Project");
}

@end

Here, in method displayMessageFromCreateOBjectiveCFile we can simply print the message for checking file is working or not.

Step 6 of 7: Connect this Objective-C file with Swift.

 For use this .m & .h files, we can connect the these Objective-C files to the Swift with the help of “Bridging-Header” file.

In this project, Bridging-Header file is “ObjectiveCWithSwiftDemo-Bridging-Header” and for any project it’s name like “projectName- Bridging-Header”

In Bridging-Header file import header file of Objective-C which is ObjectiveCFile.h which looks like:

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

#import "ObjectiveCFile.h"

 

Step 7 of 7: Create object of Objective-C Class in Swift & call the method.

 Now, in. swift file, we can create a direct object of the Objective-C class for use their methods. For that in our project, open ViewController.swift file and write a code in viewDidLoad () method :

let objObjectiveCFile = ObjectiveCFile()
objobjectiveCFile.displayMessageFormCreatedObjectiveCFile()

Which is given below:

//
// ViewController.swift
// ObjectiveCWithSwiftDemo
//

import UIKit

class ViewController: UIViewController 
{
     override func viewDidLoad()
     {
          viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.

          let objObjectiveCFile = ObjectiveCFile()
          displayMessageFromCreatedObjectiveCFile()
     }
}

Now, with the help of Step 3 of 7 build & run the project or for shortcut press cmd+b for build & cmd+r for run the project.

Woow, Your project build successfully. now same way we can import objective-c file and directly used in Swift project by following upper steps.

So, Finally we can use the Objective-C file with the help of “Bridging-Header” file in Swift Project. We can Create or Import Objective-C file in Swift.

i Hart
i Hart


This post has been seen 748 times.
  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest
  • Reddit
It is main inner container footer text