Using Objective-C Code in Swift

Yes. You can.

Image for post
Image for post
Photo by Nikhil Mitra on Unsplash

Objective-C has a massive legacy codebase, meaning the language will have a widespread for (at least) a good while yet. That might mean that you need to integrate some Objective-C code into your Swift project, and this article has been created to help you do just that.

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to create a basic iOS single view application (Guide HERE)

Terminology

Bridging: The use of a header file to enable Objective-C code to be used in a Swift codebase

Objective-C: An Object Oriented language

A demonstration in a simple project

Create a single view project with a button centred

This guide assumes that the reader can create a simple project, and instead of creating a label in the middle of the view put a button there, and add the action to your view controller.

However, to see the image in any colour other than blue the type of the button would need to be changed to custom, and the text color to something visible. An outlet is set, in this case I’ve called the outlet myButton (all shown with the animated GIF below):

Image for post
Image for post

Add a simple image to the project

We can add resources by dragging and dropping items into the project. You should drag + drop any JPEG image into the project to use as a background for the button

Add a TapGesture

Yes: There are other ways to make a button have an action, but in this case we will use a UITapGestureRecognizer that allows us to detect the use of the button:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(buttonTapped))
myButton.addGestureRecognizer(tapGesture)

Which will then call the function with the attribute @objc (guide HERE)

@objc func buttonTapped() {
MyObjcClass().printit("Print ME")
}

Now of course we need to create an Objective-C class called (imaginatively) MyObjcClass().

Add an Objective-C class

We can add a Cocoa Touch class, and specify that we want the language to be Objective-C.

The process is shown in the animated GIF as below, remembering to add the bridging header:

Image for post
Image for post

Add the Class to the Bridging Header

We need to add our class to objcswift-Bridging-Header.h, and it adds the following line:

#import “MyObjcClass.h”

Not too tricky!

Adding the Class to the Objective-C code

We then need to add the following signature to the interface (MyObjcClass.h):

- (void) printit:(NSString *)message;

and as a result we have the following function (MyObjcClass.m)

- (void) printit:(NSString *)message {
NSLog(@"%@", message);
}

which is entirely what the following animated GIF shows you:

Image for post
Image for post

The result

When we run the application, when we click the button — it prints to the console!!! Woot!

Image for post
Image for post

Conclusion:

Objective-C is a great language. You might not choose to create your project in that language, but might need to bridge to Swift in order to not rewrite all of that (tested) code. It makes sense that bridging between the two languages is easy, and this guide helps you to be able to do just that.

Extend your knowledge

  • Apple have a guide about importing Objective-C into Swift HERE

The Twitter contact:

Any questions? You can get in touch with me here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store