SwiftKotlin: Comes in handy when you want to translate Swift code into Kotlin
© Shutterstock / igor kisselev
The name of the tool is self-explanatory, for sure. SwiftKotlin is a framework, a command line tool and a Mac application for translating Swift code into Kotlin. However, keep in mind that this is not a magic bullet: its goal is *not* to produce production-ready Kotlin code, but “just a Kotlin translation that will require manual edition.”
New day, new framework! What’s great about SwiftKotlin, you ask? Simply put, it converts Swift code to Kotlin. This framework, command line tool and Mac application was developed by Angel Garcia Olloqui and “wears” the MIT license.
According to the project page, SwiftKotlin “applies transformations to the Swift code to get as correct as possible Kotlin code. It does string transformations as well as some more complicated ones like transforming
guard statements to negative
if statements, properties, memory management and many others.”
Proceed with caution
Before we dive deeper into Olloqui’s reasons for choosing Kotlin, let’s have a look at the limitations.
One cannot deny that Kotlin and Swift share some similarities, but one should not forget that these languages are different and some intrinsic differences cannot be fully translated. Furthermore, both languages “run in different environments and have access to very different frameworks and system libraries.”
Therefore, it is important to understand that this tool “does not have as a goal to produce production ready Kotlin code, but just a Kotlin translation that will require manual edition.”
Let’s take an example (adding a new item to an array has different method names):
//Swift 3 array.append("This is in Swift")
//Kotlin array.add("This is in Kotlin")
According to the project description, SwiftKotlin does not aim to map all existing methods and data types to its Kotlin counterpart but to translate the language itself. As a result, “manual editing will be required afterwards, especially when dealing with system libraries, but it is intentional and important that the developer checks the output.”
If this question looks familiar, here’s why: it’s the same question was asked when Kotlin was selected as a first-class language on Android.
SwiftKotlin (and not SwiftJava, SwiftGo or other combinations) came into being because Kotlin “is to Android/Java what Swift is to iOS/Objective-C. It adds lots of great features while at the same time it keeps complete interop to Java, which means that you can combine them both together at your best preference.”
If you are a Swift developer, you will love Kotlin. It provides the most important Swift features (optionals, extensions, generics, lambdas,…) and a very similar syntax.
If you are a Java developer, you will find in Kotlin a much more modern and safer language (optionals and mutation). It is a pleasure to write Kotlin code, much more readable and compact that the Java counterpart.
On top of that, if you consider the similarities between Swift and Kotlin, you can very easily convert code in one language to the other one and have Swift developers writing Kotlin (and vice-versa) with ease. That is, in fact, the purpose of this project, to help you do that conversion.
Getting started with SwiftKotlin
SwiftKotlin comes with 2 executable targets:
You need to make sure you run
git submodule update before building them with XCode8+;
or just go to the download page to get the most recent compiled versions of the project.
Copy the executables in a directory with executable rights. Typically, you could use:
- swiftkotlin command line tool:
- SwiftKotlin desktop app:
The project is in active development. Check it out here.