Kotlift is the first source-to-source language transpiler from Kotlin to Swift.
Kotlift helps you to write business logic once, and reuse most of the code on iOS. Porting an app from Android to iOS will be faster and less error-prone. With Kotlift, Kotlin truly is the Swift of Android.
The program is not intended to support the full Kotlin or Swift language, but most of the generated Swift code will be valid. All framework interfacing code from Android or iOS/Cocoa is not supported. Many functions from the Kotlin stdlib are the same in Swift, therefore less logic has to be rewritten.
Supported versions: Kotlin 1.0.1, Swift 2.2
The following language features are currently transpiled, but some edge cases might produce invalid Swift code.
- Variables, arrays and lists
- Basic types (String, Boolean, Double, Float, Long, Int, Short, Byte)
- Functions with parameters
- If and elvis operator
- For and while loops
- String interpolation
- Main function calling (in Swift playground)
- Custom rewrites (comment the preceding line with
// SWIFT: this.will(be.replaced)
to replace the following line) - Null safety, safe calls, null coalescing
- Basic try-catch
- @Throws annotation / throws
- Casting
- Ranges and iteration
- When / switch case
- Classes with none or one constructor
- Inheritance
- Abstract classes
- Interfaces / protocols
- Data classes (constructor and description will be generated)
- Generics
- Extension functions
- Companion objects / static class properties
- Properties with getters and setters / backing fields / computed properties
- Visibility modifiers
- Basic lambdas / closures
- Maps / dictionaries
- Sets
- Very basic smart casts
- Enums
See also the testcases in Kotlin and the generated Swift files.
The following features are currently unsupported, but are ordered by likelihood to be implemented soon:
- (Data) classes, functions or getters & setters with opening & closing brackets in same line or without brackets at all
- Line-wrapped function and class definitions
- Unnamed constructor parameter
- Unnamed function parameters (except the first one)
- Auto inferred function return types
- Finally / defer
- Full support for smart casts
- ...
The repository contains an IntelliJ project. Usage of the precompiled jar:
java -jar moshbit.kotlift.jar test-src dest replacementFile.json test-dest
- parameter: folder of Kotlin source files. (mandatory)
- parameter: destination folder where Swift code should be written to. (mandatory)
- parameter: replacement file, used for standard language replacements and may be customized. (mandatory)
- parameter: Swift testcase folder. If given, all files in the destination folder are compared to the files in this folder. (optional)
Kotlift uses various regular expressions and a simple structure tree.
For advanced Kotlift usage in your project, modify replacementFile.json to customize
replacements (such as .toString()
to .mySwiftyToStringFunction()
). Include the kotlift.swift file
in your project for a basic mapping of the most important Kotlin std-lib functions that are different in Swift.
- Kotlin 1.0.1
For every transpiled language feature there is a Kotlin test file in test-src and a Swift file in test-dest. Executing Kotlift with a fourth argument not only transpiles all test files, but also checks them for any differences.
Pull requests are welcome. If you add new features, please add Kotlin and Swift test files.
Please contact [email protected] for any inquiries.