Using Objective-C Code in Swift

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

Prerequisites:

Terminology

A demonstration in a simple project

Create a single view project with a button centred

Image for post
Image for post

Add a simple image to the project

Add a TapGesture

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(buttonTapped))
myButton.addGestureRecognizer(tapGesture)
@objc func buttonTapped() {
MyObjcClass().printit("Print ME")
}

Add an Objective-C class

Image for post
Image for post

Add the Class to the Bridging Header

Adding the Class to the Objective-C code

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

The result

Image for post
Image for post

Conclusion:

Extend your knowledge

The Twitter contact:

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