Migrating Your Objective-C Programs to Swift – A Simplified Guide for iOS Developers

The kind of popularity Swift is quickly gaining among iOS developers is enough to show just how effective this new language is. It’s a lot faster and lets you write much less code. It is strongly typed so you will face lesser crashes that are caused due to incorrect types. It resembles scripted English a lot, making it very easy to learn and work with. So if you have wanted to experiment with Swift for a while now, but are apprehensive about it, drop your worries and get right on with it.

One of the best ways to begin experimenting with Swift is to migrate an Objective-C app to Swift. Since Objective-C and Swift are very compatible, you can actually add some Swift components to an Objective-C app and it will work perfectly well. In fact, you can add new Swift features to an Objective-C app too. This way, not only will your app have some cool new features but also, this is a great way to get started with Swift.

Getting your Objective-C ready for migration

Before you begin migrating your Objective-C code to Swift, you must make sure that the two are fully compatible and to do that, your Objective-C code must be compliant with the most modern coding practices. You can quickly modernize and tidy up your Objective-C code base in Xcode, by updating components like instance type, properties, enumeration macros and object initialization. Apple tells you how to do that in detail here.

Time to get Swifty

Once your Objective-C is at its most modern, you can begin to translate your app from Objective-C to Swift, one class at a time.

Here are the steps you will take to complete the migration from Objective-C to Swift:

1. You cannot have an Objective-C subclass in Swift classes. Therefore, it’s a good idea to begin the migration process by choosing an Objective-C class that has no subclasses. You can leave the larger classes like AppDelegate for the very end.

2. Remove the .m file from a target using the file inspector. Both the .mand .h files will be replaced by a .swift file. Xcode will automatically create a header file so you don’t have to do it manually. If you need to do it manually though, you can do it by choosing File > New > File > (iOS, watchOS, tvOS, or macOS) > Source > Header File.

3. Create a single new Swift class for the Objective-C .mand .hfiles. Class prefixes are optional in Swift so you can use the same or a different name than your Objective-C class.

4. Now you need to import all relevant system frameworks. The Objective-C header you want to expose to Swift. For example:

1.#import "XYZCustomCell.h"
2.#import "XYZCustomView.h"
3.#import "XYZCustomViewController.h"

5. If you need to access Objective-C code from the same app target in your Swift file too, you need to create an Objective-C bridging header to expose those files to Swift. This is another thing Xcode makes simple for you by offering to create it when you add an Objective-C class to Swift.

6. In the.Swift file, you need to declare the class as well as make stub declarations for all members that were made public in the .h file. If you need to adopt any cocoa protocols in this class, go ahead, but be sure to provide stub protocols for each of them. if the.Swift file needs to refer to any other classes that are still declared in your target, import their .h files in your bridging header.

7.  Xcode can begin to compile right about now. Although there is no real code written yet, you won’t see the app doing much of anything, but you have a skeleton to work with now.

8. Time to get coding! You could save hundreds of hours of manually writing the code in Swift, by using a Swift translator. Be warned however that even the best Objective-C to Swift translators out there only do moderately well, and you will have to spend a long time checking and correcting mistakes line by line. This will still probably take less time than writing each line manually.

9.  Once you are done translating the entire code to Swift and have checked for errors in this .m file, it’s time to run it and test.

10. If the runtime doesn’t go as intended or worse, says it can’t find this class, check out all its references in the nib editor, enter the class’s name again in the identity inspector, save, and try again.

11. Once this works, repeat the process for all the other .m files. In the end, translate the app delegate class. By this time, there should be no Objective-C files left on the target.

So that is how you can migrate your Objective-C code to Swift. You do not have to translate every piece of your code, however. Swift may be the language of the future but Objective-C is definitely here to stay for a long time and you can certainly continue to use it. This entire exercise was only to introduce you to the world of Swift. Swift is no doubt one of the cleanest, simplest, most developer friendly programming languages and will become the language of choice for young developers starting out. You can learn it to use it in your future projects, and migrating some parts of your existing programs to Swift is a great way to begin that process. Happy coding friends.

Read also:How to implement Phone Number Verification with Sinch SDK in iOS – A Quick Guide

Checklist To Secure Your Mobile App From Every Possible Threat

Download Checklist
*We will send the checklist to your inbox and keep you updated with the latest trends and insights on app development to keep you on top of your game.

Leave a Reply