Converting Complex Objective-C Codebases to Swift

Hesham Abd-Elmegid
Aug 14, 2018 · 4 min read

The team has previously published a very thorough review of the Swiftify converter. Their review included a section on code that Swiftify failed to convert; or failed to produce the best output.

Over the past few releases we have been working on many new features and fixes to address those issues. Since we have begun working on those fixes, Swiftify has gained some great new abilities. Swiftify now has better handling of optionals, type conversions, blocks, and it is aware of your project’s class hierarchy.

The result of this is that the project used as a demo by the team is now converted to Swift with zero build-time errors!

Below are the highlights of those features and fixes. If you want to try this for yourself, download the Objective-C project and convert it with our Advanced Project Converter.

Better Handling of Blocks

Swiftify now has full support for block nullability specifiers, return type, and parameters.

Previously, this init method wouldn't have been converted correctly to Swift, resulting in code that doesn't build.

Swiftify will now convert this correctly, providing a Swift closure that has the correct parameters and return type.

Calls to this init method, that would pass a block that previously failed to be converted, can now be converted correctly too.

Now Swiftify will convert it into the following call, correctly converting the block that was passed.

Class Hierarchy Support

Swiftify is now aware of classes and interfaces that are declared in either Apple frameworks or the user code. As a result of this, Swiftify is now able to correctly detect whether override keyword should be added to the converted Swift code.

Previously, the following methods in DataSource.m would result in the converted methods having the override keyword.

Swiftify now detects the super class of DataSource.m and thus, the override keyword doesn't get added.

Skipping of self Prefix

Swiftify now intelligently skips the self. prefix for variables and methods, unless it's required by the Swift compiler (ex. conflicting local variable names, or accessing self inside blocks).

The following code would previously result in adding self. to articles and saveFavouriteArticlesToDefaults().

Now, it skips adding self.prefix as it is no longer required.

Comparison of Optionals to nil

The comparison of optionals to nil are now correctly handled by Swiftify.

Previously, the following method, which contains a nil check for set wouldn't be converted correctly.

Swiftify will now correctly do a nil check for the optional set.

Usage of dispatch_once

Usage of dispatch_once is now converted appropriately to make sure the block of code that was inside dispatch_once is only executed once.

The following method previously resulted in the converted Swift code continuing to use dispatch_once.

Now, it will replace the usage of dispatch_once, resulting in the following Swift code.

This Swift code is not ideal for all uses since it’s not thread-safe. We are currently working on converting usage of dispatch_once to a lazy-loaded variable.

Usage of Masonry

Masonry is a framework that wraps Auto Layout to provide a nicer syntax through its own DSL.

Swiftify previously couldn’t convert constraints created though Masonry’s DSL correctly. The following usage of Masonry resulted in code that did not build.

Swiftify can now correctly convert Masonry’s DSL.

Methods in Categories

In UIView+Additions.m the following method, added through a category on UIView, would be converted to Swift code that builds correctly, but results in a crash when the app is run.

Previously, the converted Swift code would return a UIView.

So when this method was called on a subclass of UIView, as follows, it would return the wrong type, resulting in a crash.

This has been fixed. The converted code now returns Self, so it would work correctly on any subclass of UIView.

Convert Your Project

Swiftify’s Advanced Project Converter can now convert entire projects or multiple files at once. With support for advanced language features, this will significantly reduce the amount of work you’ll have to do to convert your Objective-C code to Swift.

Install Swiftify for Xcode from the Mac App Store and give it a try.

To see the converted Swift code and the original Objective-C project, check this repository on GitHub.

For more guidance, read our post on Swiftify’s Advanced Project Converter, and please share your questions in the comments section below.


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store