Learn the compatibility of Swift and Objective-C

Tram Ho

Learn the compatibility of Swift and Objective-C

With the introduction of Swift in 2014, many organizations are trying to convert their source from Objective C to Swift. But with systems that are too large to convert, interoperability is required between Swift and Objective C. For those who are starting a new project, they can directly jump into Swift but many libraries still use Objective. C. Therefore, there is no other option than using both languages ​​in a single library / project. In this article, I will introduce to everyone about Objective-C interactions in Swift | Swift in Objective C in the sample code and some examples.

First, you need to download the example project to get started: https://github.com/mushtaque87/SwiftObjC

Objective C in Swift

Suppose you have created a new SwiftObjC Project. And we need to get an Objective C / TestObjC.h class into the project quickly. Create a new .m file in the project

Select Create Bridging Header.

After the previous steps, we can see both TestObjC.m and SwiftObjC-Briging-Header.h files in the directory structure:

Now add this line of code in the SwiftObjC-Briging-Header.h file.

Now we will add the .h file (Interface File) in the project. Select File -> Select the .h file, name it TestObjC.h and then add the following code:

So now we have all the code and bridges to use Objective C properties and methods in Swift. In any Swift class you want to use Objective C code and access the following properties and methods:

The Swift compiler will automatically import Objective C classes into swift classes.

Swift in Objective-C

It will be a bit difficult because we’re going backwards, but don’t worry, let’s get started! Create an Objective C profiect (ObjcSwift) and add Swift file (TestSwift.swift). When adding files, you will also be prompted to create a bridging header.h file as above. Select Create a Bridging Header Now the project directory structure will look like this:

In this case, you don’t need to add any code in the Bridging header.h file. Now, add these lines of code to the newly created Swift file:

If you want to use any Objective C file, you must enter <ProductModuleName-Swift.h> in the ObjectiveC file (.m). You can find productModuleName in the Build Settings section

The Swift compiler when building the project created an interface .h file for the swift class not shown. You can see this compliant interface with Command + on this line:

Scroll to the end and you can see the custom and variable class like this:

Now in File Objective C, you can use the Swift file deployment like this:

Back in your TestSwift.swift file, you can see the code through the testFunction method:

This is important because Objective C does not understand advanced Swift classes. This line will create the interface shown in the screenshot above in the pre-compiled swift.h file that Objective C understands and can call. If this line is missing, you may receive the following error.

For more information refer to these Apple documents: https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c#overview

Share the news now

Source : Viblo