Swiftpack.co - Package - angelolloqui/SwiftKotlin

status Swift 5 Swift Package Manager License Twitter


A tool to convert Swift code to Kotlin in a very easy and quick way.

What is this?

SwiftKotlin is a framework, a command line tool and a Mac application for translating Swift code into Kotlin.

It applies transformations to the Swift code to get as correct as possible Kotlin code (see limitations below). 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.

It comes with a desktop Mac application to copy & paste your code, as well as a command line tool to transform a complete project to Kotlin.

Why use SwiftKotlin?

Why Kotlin?

Kotlin is a great language, it 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 with Java, which means that you can combine them both together as you prefer.

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. You can check a side by side comparison of Swift and Kotlin here.

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 than the Java counterpart. You can check some of the benefits and differences in Kotlin official documentation and a nice list of reasons to chose Kotlin over Java.

Moreover, Kotlin is fully integrated in IntelliJ (Android Studio), so you can keep using the "de facto" IDE for Android with all the added benefits brought by Google, and it even has a Java to Kotlin converter if you want to update your legacy Java.

On top of that, if you consider the similarities between Swift and Kotlin, you can 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 with that conversion.

Benefits over shared code across platforms

There are many alternatives for making multiplatform projects or sharing code between them. Some alternatives are Xamarin, ReactNative, Cordova or low level C++ libraries.

The main issue with all of them is that once you chose to use them, you need to keep in their boundaries, including specific tools and libraries, introducing a steep learning curve and a big risk in terms of dependency on that 3rd party. Besides that, for many of those options the resulting app will lack the quality of a fully native app.

On the other hand, by using Kotlin, you will still have 2 fully native applications, with all the benefits (best quality, performance, best tools per platform -Xcode/Android Studio-, follow platform conventions,...), but at the same time minimize the extra work required to translate between them due to the similarity with Swift.

In fact, I explored an actual example using MVVM+Rx, where I got between a 50% and 90% of code similarity depending on the layer (non UIKit dependent is much more reusable than UIKit dependent classes of course). It took me around 30% the time to convert the Android version from the iOS version and I did not have SwiftKotlin then. ;)


Despite the similarities, Swift and Kotlin are different languages, with some intrinsic differences in them that cannot be fully translated. Besides that, they both run in different environments and have access to very different frameworks and system libraries.

Because of that, this tool does not have as a goal to produce production ready Kotlin code, but just a Kotlin translation that will require manual editing. For example, things as simple as adding a new item to an array have different method names:

// Swift
array.append("This is in Swift")
// Kotlin
array.add("This is in Kotlin")

The scope of this project is not mapping all existing methods and data types to their Kotlin counterpart, but to translate the language itself. This means that manual editing will be required afterwards, especially when dealing with system libraries. It is intentional and important that the developer checks the output.


The project is in active development, with many rules and improvements still to be implemented. Some of them include:

  • ☑ Constructors
  • ☑ Simple Control flow statments (guard, if, for, while, switch)
  • ☑ Composed Control flow statments (multiple guard, if let)
  • ☑ Extensions
  • ☑ Keyword replacements (val, this, fun, ...)
  • ☑ Basic memory management (weak, captures,...)
  • ☑ Function returns and named parameters
  • ☑ Basic property transfromers (getters and setters)
  • ☑ Lazy properties
  • ☑ Properties with didSet/willSet
  • ☑ Static to Companion
  • ☑ Struct to data class
  • ☑ String interpolators
  • ☑ Foundation types (arrays, maps,...)
  • ☑ Basic exception handling syntax
  • ☑ Simple enums
  • ☑ Complex enum cases to Sealed classes
  • ☐ ... (check open issues)

With the implemented rules you can already get pretty decent Kotlin output for many of your classes. The rest will come in future releases.


The project comes with 2 executable targets:

  • SwiftKotlinCommandLine
  • SwiftKotlinApp

Checkout the project's releases page to get a preview compiled version of the app or compile it manually:

  • Install Xcode 11+ and check it is the default selected one by running xcode-select -p
  • clone the repo: git clone git@github.com:angelolloqui/SwiftKotlin.git; cd SwiftKotlin
  • Run swift package update and swift package generate-xcodeproj --enable-code-coverage
  • Open the project: open SwiftKotlin.xcworkspace

From Xcode, run the desired target and copy the generated executables (you can find it under the Products generated folder) in a directory with executable rights for later use. Typically, you could use:

  • swiftkotlin command line tool: /usr/local/bin/
  • SwiftKotlinApp desktop app: /Applications/

NOTE: If you upgrade any project dependencies, make sure you run: swift package generate-xcodeproj --enable-code-coverage after upgrade


Command line tool

If you placed swiftkotlin in any of your path directories, just run: swiftkotlin [<file>] [--output path]

Note that you can specify a directory as input. Output will by default use the input directory, creating a <name>.kt file for each existing <name>.swift file found.


MIT licensed.


Forks, patches and other feedback are always welcome.

For a list of missing features, check the disabled Unit Tests.


SwiftKotlin uses Swift-Transform and Swift-AST for generating the AST for transpilation.

SwiftKotlin is brought to you by Angel Garcia Olloqui. You can contact me on:

And special credits for the contributors:


Stars: 2661


Used By

Total: 0


UIKit transforms and Swift5.2 fixes - 2020-05-17 16:19:39


  • New transforms plugin with very simple mappings between UIKit and Android widgets
  • #116: Swift 5.2 implicit return fixes

Enum improvements - 2020-03-29 14:54:34

Added several minor improvements on enums:

  • #112: Removed redundant . in switch expressions
  • #113: Fixed wrong addition of in in switch expressions
  • #114: Added is to enums when using sealed classes with tuples
  • Removed Equatable conformance from sealed classes enums

Equatable and other fixes - 2020-03-21 17:40:17

Maintenance release including the following fixes: #101: Fixed type inheritance in structs #110: Removed Equatable type inheritance in structs #108: Fixed equatable enums Fixed wrong listOf conversion

Swift5 and other improvements - 2019-11-08 20:25:46

Updated to Swift5 and Xcode 12, together with the following: #48: Unicode support on strings #104: Transformed private setter properties #82: Fixed bug on guard condition inversion

Enum improvements, dark mode and other bug fixes - 2019-05-06 08:22:11

Improvements and resolved issues:

  • #85: Dark theme for App
  • #91: Force cast conversion
  • #94: Improved enums with rawValue init
  • #90: Override methods remove default parameters
  • #68: List and map conversion improvements

Swift update to 4.2 and other improvements - 2018-12-09 17:53:09

Improvements and resolved issues:

  • Changed project structure to allow easier dependency update
  • Updated code to use Swift 4.2 and Xcode 10
  • #76: Keep code comments
  • Other minor updates and fixes

Better properties, lambdas and bug fixes - 2018-03-24 12:26:44

Improvements and resolved issues:

  • #47 Added support for lazy properties
  • #22 Added support for didSet/willSet properties
  • #29 Added support for varargs
  • #69 Improved return statements in lambda expressions
  • #70 Fixed issue with computed properties in structs

Code optimizations, bug fixes and small improvements - 2018-02-03 16:19:32

Resolved issues and improvements:

  • #26 Removed optional chaining in lambdas
  • #20 Optimized body returns to single line
  • #53 Added inheritance support to enums
  • #60 Fixed position of generics in function definitions
  • #41 Added invoke to optional lambdas

Bug fixes and minor improvements - 2017-12-10 19:14:47

Resolved issues:

  • #14 Add support for fatalError()
  • #25 Optional chaining in Kotlin requires optional checks in all expressions
  • #56 Switch cases with multiple lines not properly indented by {}
  • #55 Remove underscore assignments

Sealed classes, guard statements and other improvements - 2017-11-28 22:26:29

Resolved issues:

  • #49: Add feedback error messages when Swift is incorrect into SwiftKotlin App
  • #46: Typing double quotes into SwiftKotlin App becomes curly quotes
  • #45: Allow Semicolons in Swift Code
  • #32: Translate complex enums to Sealed classes
  • #21: Optimize guard unwrapping expressions
  • #28 and #37: Removed unnecessary annotations in Kotlin

Simple enums, ternary expressions and other improvements - 2017-11-01 18:45:12

Resolved issues:

  • #33: Transform ternary expression enhancement
  • #30: Translate simple enums to Kotlin enhancement
  • #24: SwiftKotlinApp changes text selection while writing bug
  • #23: Initialize nullable properties to null bug
  • #17: Remove -> from lambdas when no parameter enhancement

Support for XCTestCase - 2017-10-15 16:54:00

Added plugin support, including:

XCTestCase to JUnit plugin:

  • Adding @Test, @Before and @Afterannotations, removing calls to super.setUp()and super.tearDown()when applies
  • Removing inheritance of `XCTestCase``
  • XCTAssertXXX methods to JUnit conterparts, changing parameter order when necessary

SwiftKotlin App Preview - 2017-09-09 10:41:25

Preview of SwiftKotlinApp using the new transformer based on AST

0.0.1 - Preview - 2017-06-10 15:55:51