It’s been quite sometime since Swift has come in the market. As it provides many advantages over Objective-C, developers are moving their legacy Objective-C code to Swift. When I did it first, I thought it was better to re-write the app from scratch again in Swift. But it is possible to migrate the same legacy code to Swift with little or less hassle. You can actually improve the architecture, logic and performance by replacing pieces of Objective-C to Swift. The following steps will make the code better and also you can migrate the code from Objective-C to Swift very swiftly and easily. Let’s begin
Clean Up Your Code
The first thing is to make sure that the Objective-C code and Swift code have best or most favourable compatibility. You can do this by modernizing and cleaning up the existing Objective-C code. For example, if you find out that there are parts of the code which don’t have nullability annotations, now is the time to add them. Your code must follow modern coding practices. This is to make sure that Objective-C interacts with Swift without any problem and the communication is effective.
Migrate The Code
The best way to do this is to migrate to Swift one class at a time. It is best to start with the classes that don’t have any subclasses. The reason behind this is that you can’t subclass Swift classes in Objective-C. Replace the .m and .h files for that class with a single .swift file. Whatever you have in your implementation and interface files will directly go into this single Swift file. There’s no need to create a header file since Xcode automatically generates a header in case you need to referent it.
Create a Swift class for your corresponding Objective-C .m and .h files by choosing File > New > File > iOS, watchOS, tvOS or macOS > Source > Swift File. You can use the same or a different name that your Objective-C class. Class prefixes are optional in Swift.
Import all relevant system frameworks.
Complete the Objective-C bridging header if you need to access Objective-C code from the same app target in your Swift file.
To make your Swift class accessible and usable back in Objective-C, make it a descendant of an Objective-C class. To specify a particular name for the class to use in Objective-C, mark it with @objc<name>, where name is the name that your Objective-C code uses to reference the Swift class.
As You Migrate
You can set up your Swift class to integrate Objective-C behaviour by subclassing Objective-C classes, adopting Objective-C protocols and more.
As you work with Objective-C APIs, you will need to know how Swift translates certain Objective-C language features. For these features you can see THIS.
Use the @objc<name> attribute to provide Objective-C names for properties and methods when necessary.
You can denote instance methods (-) with “func” and class methods (+) with “class func” respectively.
Declare simple macros as global constants and translate complex macros into functions.
Update import statements in your Objective-C code to refer to the new Swift code.
Remove the original Objective-C implementation file from the target by deselecting the target membership checkbox. Don’t yet delete the .m and .h files immediately. You might still need them to troubleshoot.
Update your code to use the Swift class name instead of Objective-C name if you gave the Swift class a different name.
Troubleshooting Tips and Reminders
Migration experiences can differ based on your existing code. Here are some general steps and tools to help you troubleshoot the process.
A Swift class cannot be subclassed in Objective-C. The class you migrate can’t have any Objective-C subclass.
When you migrate a class to Swift, the corresponding .m or the implementation file must be removed from the target as it will send you a duplicate symbol error when you build.
You’ll need to make your Swift file a descendant of the Objective-C class to make it available in Objective-C.
Command-c a Swift class name to see its generated header.
Option-click a symbol to see implicit information about it, like its type, attributes and documentation.