Here’s How to Migrate from Objective-C to Swift

Manish Barthwal
Dew Solutions
Published in
4 min readOct 18, 2021

Swift and Objective-C are the two primary programming languages used for iOS app development. App developers generally prefer the former over the latter for the obvious reasons that it is relatively new, involves writing less code, contains lesser bugs, accelerates app development, possesses high security, and more. In fact, Apple released Swift as a replacement for Objective-C. This is one of the key reasons why iOS developers have started migrating or moving apps to Swift from Objective-C. Some of the popular apps like LinkedIn and Yahoo have already moved while many others are thinking about the same.

Ever since its release in 2014, the Swift programming language has undergone several iterations and continues to rise in popularity among the iOS developer community. That being said, developers who previously worked with Objective-C are now adopting Swift to stay more updated.

If you too have your iOS apps built in Objective-C, you should definitely consider the transition to Swift for added advantages. One can improve the architecture, performance, and logic of an Objective-C app by replacing pieces of it in Swift. This is because of the interoperability feature that makes it possible to integrate features migrated to Swift into Objective-C code without any hassle. iOS developers do not require rewriting the entire application in Swift.

Before you begin with the migration process, make sure that you are using the latest Objective-C code. It should follow modern coding practices so that it interacts more effectively with Swift. With Xcode- the modern Objective-C converter, you can implement the correct enum macros and change the id to instance type whenever possible. This also helps you in updating to the more modern @property syntax.

To use the Objective-C converter, click on Edit > Refactor > Convert to Modern Objective-C Syntax.

iOS app developers need to be vigilant about the convertor as although it helps with the mechanics of identifying and applying potential modernizations, it does not interpret the semantics of the code. Make sure to manually check and confirm any changes that the converter asks to make to your code before going ahead with the Objective-C to Swift migration.

Code Migration Process

The process to migrate your code to Swift is most effective when done on a per-file basis, i.e. one class at a time. Since there is no option to subclass Swift classes in Objective-C, it is recommended that you choose a class in your application that doesn’t have any subclasses. You are then required to replace the .h and .m files with a single .swift file. Everything from your implementation and interface goes directly into this single file. You would not be creating a header file as Xcode generates it automatically in case you need to reference it.

Creating a Bridging Header File

While adding your first .swift file to the project, you may come across a prompt that says “Would you like to configure an Objective-C bridging header?” Click on “Create Bridging Header” and in case you dismissed the prompt or didn’t get one, you can add a new .h file to your project and name it [MyProject]-Bridging-Header.h

Make sure to link its path in your target’s project setting.

You may wonder whether you need to convert all your Objective-C code to Swift. No, you do not. You may want to leave a few pieces of code in the former, and that’s absolutely fine.

A step-by-step process to shift from Objective-C to Swift

For a smooth and hassle-free migration process, pay attention to the following points-

1. Select a pair of .h (MyViewController.h) and .m (MyViewController.m) files that need to be converted. In case you want to convert the entire project, leave the AppDelegate class for last.

2. Look for #import “MyViewController.h” throughout your project.

3. Remove #import “MyViewController.h” from the Objective-C Bridging Header File ([MyProject]-Bridging-Header.h)

4. Replace instances of #import “MyViewController.h” with #import “[MyProject]-Swift.h” in all the .m files. Similarly, in all the .h files, replace #import “MyViewController.h” with a forward class declaration: @class MyViewController. This helps you steer clear of circular references between header files.

5. Using the Finder extension which is included in Swiftify for Xcode, convert the pair of Objective-C files to Swift. Alternatively, copy the contents of both .h and .m files into the Swift file and head over to the “Convert File to Swift” command in the extension.

6. Remove .h and .m files from the project and add the new converted .swift file.

7. Try compiling the project and fix any bugs or errors that occur. Several issues can be fixed just by Xcode auto-fix suggestions. The “Editor > Fix All in Scope” command is quite helpful here. Contact Swiftify if an error occurs very often.

8. Now, it’s time to build and run the project. If you encounter any issues during this process such as the runtime complains it can’t find the class, look for the references in the Storyboard Editor. Re-enter the class name in Identity Inspector and then try once again after saving it.

9. In the very first point, we talked about leaving the AppDelegate class if you are converting the entire project. After all the files have been converted, now you can convert the AppDelegate class. Up until now, if the target is left with no Objective-C files, you can delete the main.m file and the .pch file.

That’s all we have on how to migrate or convert to Swift from Objective-C.

Originally published at https://www.dewsolutions.in on October 18, 2021.

--

--

Manish Barthwal
Dew Solutions

Passionate writer with a keen interest in Technology