Converting a full objective-C app to Swift

Naveen Saraswat
Sodio Technologies
Published in
6 min readApr 11, 2018
Converting a full objective-C app to Swift

Converting a full objective-C app to SWIFT provides opportunities to improve the architecture, efficiency, and logic of the existing iOS apps. Pieces of Objective-C code can be migrated into Swift to carry out the aforementioned conversions. If you are looking to convert your whole project at once, it can also be done

Difference between Objective-C and SWIFT

Apple launched a new programming language called SWIFT in 2014 for iOS and Mac OS development. It is an object-oriented and functional language, designed especially to assist in app development for Apple devices. It differs from objective-C in many areas, such as-

  • WYSIWYG interactive editor as compared to C’s basic editor
  • Smaller code lengths
  • Better readability as compared to Objective-C’s code
  • Simplified and faster execution time
  • Resembles the English language

Why SWIFT?

As mentioned earlier, Swift has many advantages over Objective-C. Apple is looking to shift their focus to app development over to Swift usage, instead of the cumbersome objective-C. With that being said, Swift has really caught up with developers as well. It has overtaken objective-C in popularity, and, according to recent reports, 77% of developers prefer Swift over Objective-C for iOS apps development.

Also Read: Points to Consider before developing an iOS App

Existing tools aren’t really efficient

A simple search of ‘Objective-C to Swift converter’ will give you a handful of tools that can automate the process of converting a full objective-C app to SWIFT. Online tools will automatically convert your Objective-C code to Swift code. When tested on small files, it works satisfactorily, but, as expected, on big projects it throws a lot of bugs and problems.

  • Double definitions of variables
  • not null checks
  • wrong optionality
  • styling issues

All or many of these can be encountered while swift conversion of iOS apps, depending upon the complexity and length of your project. To some users, converting a full objective-C app to Swift might sound like a fundamentally bad idea, using a converter, even more so. Thus, it is better suited to convert the code manually from scratch.

https://sodio.tech

Converting the code from scratch

When it comes to straightforward and simple migration of apps, online tools do the job. When it comes to large projects, extensions and online tools fail, as expected, throwing too many exceptions and build errors. Here is how we can manually convert the code:

Preparation of project for migration
Before migrating the project, we need to make it Swift compatible, such as replacing ID with instance type, updating to the @property syntax, and deploying the right enum macros. Once you carry out all these steps, be sure to manually run through your project and check for any unwanted errors or bugs. This step is crucial for converting a full objective-C app to Swift.

The migration process
Because you cannot create subclasses of Swift classes in Objective-C, you might believe that converting a full objective-C app to Swift is a tough job, but it is best to carry out the conversion on a per-file basis, that is, one class at a time. XCode generates a header file automatically if you need it. The .m and .h files are replaced with a .swift file so that everything from the implementation of the project goes directly into the .swift file. The next steps are to create a bridging header, carrying out the actual process, and then finally testing the app to see if it performs as desired.

Creating the bridging header
A bridging header file is needed in order to integrate objective-C code seamlessly into Swift. When you add your first .swift file to the project, you will automatically see a dialog box open, that will prompt you to create a bridging header file. It automatically creates the file and links its target path to your project settings.

If for some reason, the prompt does not pop up, you could create a header file manually, and then do the path linking process on your own. The bridging header is crucial for converting a full objective-C app to Swift, as it enables seamless integration of Objective-C code into Swift.

Converting a full objective-C app to Swift

The actual steps
Once you’ve decided what parts of your code you want to convert, or if you want to convert the whole project, it is time to get into the step-by-step action of converting a full objective-C app to Swift.

  • Start with one class at a time. Choose the .m and .h files that you want, and if you are converting the entire project, choose the AppDelegate class last.
  • Search for the import command of your chosen files in the project, and remove them from the Objective-C bridging header file. The import command looks like ‘#import “filename.h”.
  • For all the .m files, replace the import command with the appropriate command for importing .swift files, to import your swift umbrella header.
  • For all the .h files, replace the import command with a forward declaration for the class, i.e. @class classname. This is to prevent circular references.
  • Now, convert these .h and .m files to Swift using the XCode finder extension. This will give you a .swift file, add this to your project and delete the .m and .h files.
  • Compile the project, and fix any errors that occur. When all the errors have been fixed, try building and running the project to see if there are any runtime errors or crashes that affect your project.
  • If you were converting the entire project, convert the AppDelegate class (which we left in the beginning). Delete the main.m file and the .pch file.

Testing phase
If your project is building and running, try different test cases to see that it satisfies all of them, and there have been no issues in the swift conversion process. You could also write your own tests in Swift to see if your code works properly, this can help in fixing stray logical or semantic errors that might have been overlooked. When all is said and done, you have successfully completed the process of converting a full objective-C app to Swift!

Advantages

  1. Considerably less number of build errors and/or bugs.
  2. A better understanding of the code conversion process.
  3. Builds a better understanding of both languages, which is necessary for competitive developers
  4. Runtime errors/glitches ironed out in the testing phase

The final summary

Converting a full objective-C app to Swift is not a very tough job, especially with the tools and extensions available today. Online tools do the job of swift conversion not so well, with the automated converter working on smaller projects. But, as we need to convert larger projects, we prefer the manual conversion method to all extensions or tools available online.

Where Sodio can help

We have a talented team of 20+ developers that can assist you in converting a full objective-C app to Swift code. Our experience in mobile app development lends us a significant advantage in this field, as we can get the job done quicker and more efficiently. With having published over 350 projects for both iOS apps and Android, we are bound to leave you satisfied with our work.

Feel free to get in touch!

More from our Tech blog

  1. Points to consider before Developing an iOS App
  2. How to make an App for iPhone X?
  3. How to make Android and iOS Apps?
  4. Is the App for that era over?

--

--