A deep dive into Swiftify
What is Swiftify?
Put simply, it’s a tool for converting your Objective-C code to Swift with the click of a button. This could be applied to whole projects and not just code snippets, which means that Swiftify is aware of your project’s structure. What it also means is that Swiftify is not just a dumb tool that converts Objective-C’s messaging syntax to Swift’s dot syntax; it’s much more complex than that, with more than 4 years in development, it has reached a point where it can convert very complex Objective-C code to its Swift equivalent while maintaining the logical correctness of your code. As a matter of fact, Swiftify has become so good at its job that Apple has dubbed it The Swiftest way to Swift when it was featured on the AppStore.
Anyone familiar with Objective-C, as you probably are considering you’re reading this article, knows that its syntax is not the most common among programming languages and, therefore, produces a lot of challenges in understanding and translating its structure into code.
So here is why I use Swiftify for converting any Objective-C code I have:
Packed with Features
From simple, online conversions of code snippets, to a fully fledged enterprise tool for offline project conversions! Here is a detailed list of all the features that Swiftify offers:
1. Xcode Extension
With minimum setup, you’ll have Swiftify integrated into Xcode and you’ll be able to Swiftify code selections and files; however, this cannot happen with multiple header and source files at once. Finally, you’ll be able to set a shortcut for any command such as Paste as Swift, which lets you paste previously copied Objective-C code as Swift — pretty cool, right?
2. Integrate with Finder
Integrating with Finder allows you to convert files from your Mac’s Finder by right-clicking on the file → Convert File(s) to Swift using Swiftify. You will also be able to convert code selections from most macOS text editor apps.
Unlike Xcode, the Finder extension supports merging declarations from header and source files, thus complementing the Xcode extension.
You can check this link to see how to install both the Xcode and Finder extensions. Both of these features are included in the Pro Subscription Plan. Make sure to check the yearly subscription, which offers huge discounts over the monthly plans.
3. AppCode Plugin
If you’re using JetBrain’s AppCode as your main IDE, Swiftify has you covered as well! On top of that, this plugin is more advanced than its Xcode peer, and it can convert multiple source and header files at once! You can say it is almost identical to the Advanced Project Converter, which was created to cover the shortcomings of the Xcode extension.
4. Advanced Project Converter
The Advanced Project Converter lets you convert individual files, groups of files, or entire projects with a desktop UI that is easy to use.
It is important to note that The Advanced Project Converter was created to deal with the Finder/Xcode extensions’ shortcomings, such as:
- Merging declarations from header and source files
- Updating the Xcode project file
- Creating or updating the Objective-C Bridging Header for the newly added swift files
You can see how to install and use the Advanced Project Converter here.
5. Offline Converter
This is an Enterprise solution for companies (or individuals) who would like a more secured conversion of their code and, as an added bonus, huge performance improvements compared to the online converter! You can read all that you need to know about the offline converter here.
Improved, More Powerful Swiftify
The next few months are looking very bright for Swiftify. The team is hard at work, always figuring out new ways to help make your lives easier when it comes to Objective-C to Swift conversions. However, the dates specified in the roadmap below may change due to our Agile work nature, new features may replace mentioned ones, and others may be dropped or pushed further down the road.
As we mentioned earlier, the conversions performed by Swiftify are not dumb search and replace operations on Objective-C’s syntax, but, instead, the tool is more like a compiler that:
- scans one character at a time.
- executes Lexical Analysis on the code by tokenizing the characters that were scanned.
- then performs Syntactical Analysis to ensure that the tokens produced from the lexical analysis are in proper order.
Semantic Analysis then generates an intermediate language or structure so that it can be converted into its Swift equivalent.
This complex process gives Swiftify an impressive advantage, which is data type awareness.
gets converted to:
If Swiftify wasn’t aware of
i’s data type, and the addition was converted to a mere
var g = i + f , it would have produced a compiler error saying that
Binary operator '+' cannot be applied to operands of type 'Int' and 'Float' ;.
You can explore the free and online code converter here. This alone can show you how smart the tool really is. Take as another example the following code snippet, which adds a persistent store to an NSPersistentStoreCoordinator:
gets converted to:
Here you can see that Swiftify was able to wrap the call in a
try because this function
throws, and, on top of that, it was also able to infer that the
persistentStoreCoordinator is an optional property and added an optional unwrapping to the property!
There are many other such examples that you can find here.
With Swiftify you can also:
- generate Swift documentation.
- increase the user base for your libraries.
- migrate your legacy code.
If you have any questions, please don’t hesitate to ask them in the comments section below.