This tutorial will guide you through the process of calling Objective-C code from Swift and vice versa.
Swift and Objective-C are interoperable, allowing you to seamlessly use code written in Objective-C within your Swift projects. Integrating Objective-C code into a Swift project can be essential when working with legacy code, third-party libraries, or existing projects.
Here’s a step-by-step guide on how to call Objective-C code from Swift:
Using Objective-C Classes in Swift
This section outlines the steps to incorporate Objective-C code into a Swift project.
Add Objective-C Implementation (.m)
Create a new file named CustomObject.m
and add your Objective-C implementation.
// CustomObject.m #import "CustomObject.h" @implementation CustomObject - (void)someMethod { NSLog(@"SomeMethod Ran"); } @end
This code defines an Objective-C class named CustomObject
with a method called someMethod
. This method simply logs a message when called.
Add Bridging Header
- Upon adding the .m file, Xcode may prompt you to configure an Objective-C bridging header. Click “Yes.”
- If not prompted, create a new .h file named
<#YourProjectName#>-Bridging-Header.h
. - Link the header path in your project settings using $(SRCROOT) for flexibility.
Add Objective-C Header (.h)
Create a new file named CustomObject.h
and define your Objective-C class with properties and methods.
// CustomObject.h #import <Foundation/Foundation.h> @interface CustomObject : NSObject @property (strong, nonatomic) id someProperty; - (void)someMethod; @end
This header file declares the CustomObject
class, including a property someProperty
and a method someMethod
.
Build Your Objective-C Class
In CustomObject.m
, implement the methods as needed.
Add Class to Bridging-Header
In <#YourProjectName#>-Bridging-Header.h
, import your Objective-C header.
// <#YourProjectName#>-Bridging-Header.h #import "CustomObject.h"
This ensures that Swift can recognize and use the CustomObject
class.
Use Your Object in Swift
In your Swift file (SomeSwiftFile.swift
), create an instance of CustomObject
and use it.
// SomeSwiftFile.swift var instanceOfCustomObject = CustomObject() instanceOfCustomObject.someProperty = "Hello World" print(instanceOfCustomObject.someProperty) instanceOfCustomObject.someMethod()
This Swift code creates an instance of CustomObject
, sets a value to its someProperty
, prints the value, and then calls the someMethod
method.
Using Swift Classes in Objective-C
This section explains how to use Swift code in an Objective-C project.
Create New Swift Class
Create a new Swift file named MySwiftObject.swift
and define your Swift class. Annotate it with @objc
to make it accessible in Objective-C.
// MySwiftObject.swift import Foundation @objc(MySwiftObject) class MySwiftObject: NSObject { @objc var someProperty: AnyObject = "Some Initializer Val" as NSString override init() {} @objc func someFunction(someArg: Any) -> NSString { return "You sent me \(someArg)" } }
This Swift class, MySwiftObject
, is marked with @objc
to make it accessible from Objective-C. It has a property someProperty
and a method someFunction
that takes an argument and returns a string.
Import Swift Files to Objective-C Class
In your Objective-C file (SomeRandomClass.m
), import the automatically generated Swift header.
// SomeRandomClass.m #import "<#YourProjectName#>-Swift.h"
This imports the Swift header file that Xcode generates automatically based on your Swift files.
Use Your Class in Objective-C
Create an instance of your Swift class and use it in Objective-C.
// SomeRandomClass.m MySwiftObject *myOb = [MySwiftObject new]; NSLog(@"MyOb.someProperty: %@", myOb.someProperty); myOb.someProperty = @"Hello World"; NSLog(@"MyOb.someProperty: %@", myOb.someProperty); NSString *retString = [myOb someFunctionWithSomeArg:@"Arg"]; NSLog(@"RetString: %@", retString);
This Objective-C code creates an instance of MySwiftObject
, sets and reads its property, and calls its method, demonstrating the interoperability between Swift and Objective-C.