Raphael Stäbler

Raphael

profile-pic
App Development

Kotlin for Swift Developers

Whether you are an iOS developer wanting to learn more about Android development, or you’re an Android developer working with Java, or even if you’re an Android developer using Kotlin and you want to learn more about Swift, this article is for you.

It’s going to be a comparison of two popular and modern languages used for mobile app development. I’m going to talk languages only, though, and I won’t go into actual app development stuff. So, what you’ll see here will be applicable outside the Android and iOS world as well.

Similarities of the two

First of all, both languages have a lot in common. They both are strongly typed and they support type inference, so you don’t have to specify a variable’s type when it can be derived from its value:

var text: String = "lorem ipsum" // This is a string variable
var otherText = "sit dolor amet" // This one is also a string

They also both support immutables, which is quite a powerful concept and allows for thread-safe coding:

let numberFour = 4 // Immutable integer in Swift
val numberFive = 5 // Immutable integer in Kotlin

They both support null safety using Optionals:

// someMethod will be called if the object isn't null
someObjectOrNull?.someMethod()

They both allow for end-positioned function arguments to be appended to function calls.

Swift:

// Define function
func doSomething(andCall callback: () -> Void) {}
// Call function
doSomething {
    // Callback function body}

The same with Kotlin:

// Define function
fun doSomething(callback: () -> Unit) {}
// Call function
doSomething {
    // Callback function body}

All of those things allow for clean and readable code in both languages.

Now, let’s look at some of the differences.

if let vs ?.let

A well known and often used pattern in Swift is the if let pattern where you’ve got an Optional and you only need to work with it when its value is set:

let someOptional: String? =if let text = someOptional {
    // in here, text is a non-optional string}

You can do the same with Kotlin using the standard function let that is defined on every type:

val someOptional: String? = …
someOptional?.let { text ->
    // in here, text is a non-optional string}

There’s no equivalent to Swift’s guard let in Kotlin, though. In return, Kotlin’s standard functions (e.g. let, apply, with, run, also, …) have use cases far beyond the provision of if let functionality and going into them would be outside the scope of this article.

Literals vs helper functions

Like many languages, Swift knows literal notations for complex types like Arrays and Dictionaries:

// Array of type [String]
let list = ["lorem", "ipsum"]
// Dictionary of type [Int: String]
let dict = [10: "lorem", 20: "ipsum"]

Unfortunately, Kotlin doesn’t have a literal notation like this — at least not yet. Instead, you need to fallback on helper methods:

// Array of type Array<String>
val list = arrayOf("lorem", "ipsum")
// Map of type Map<Int, String>
val map = mapOf(10 to "lorem", 20 to "ipsum")

There are several of these helper functions for different cases: arrayOf, mutableArrayOf, listOf, mutableListOf, setOf, mutableSetOf, mapOf, mutableMapOf.

Function naming and named parameters

Swift has kind of inherited its function declarations from Objective-C it seems. That’s probably because Apple wanted their existing APIs to work similarly in both languages. Anyway, I personally like the way function declarations work in Swift because it can lead to very readable code:

// Define function
func processText(_ text: String, withOptions options: [Int]) {}
// Call function
processText("lorem ipsum", withOptions: [1, 2])

In comparison to this, Kotlin uses a more classical, C-like approach:

// Define function
fun processText(text: String, options: Set<Int>) {}
// Call function
processText("lorem ipsum", setOf(1, 2))

There is, however, a possibility to use named parameters in function calls:

// Name all parameters
processText(text = "lorem ipsum", options = setOf(1, 2))
// Name only second parameter
processText("lorem ipsum", options = setOf(1, 2))

The second variant comes pretty close to Swift’s descriptiveness.

it

The it keyword in Kotlin is similar to $0 in some of Swift’s higher order functions. Let’s take a simple map call as an example:

[1, 2, 3].map { $0 * 2 } // Results in [2, 4, 6]

Now the same with Kotlin:

listOf(1, 2, 3).map { it * 2 } // Results in listOf(2, 4, 6)

In addition to this, a lot of Kotlin’s standard functions work with it. Let’s revisit our if let example from earlier:

val someOptional: String? = …
someOptional?.let { text ->
    // in here, text is a non-optional string}

If we wouldn’t have named our parameter text inside the let block, the unwrapped content of someOptional would be accessible as it:

val someOptional: String? = …
someOptional?.let {
    // in here, it is a non-optional string}

Conclusion

Swift and Kotlin do share a lot of similar concepts yet they still take different approaches in many cases. I hope I could help you bridge some of the gap between those two remarkable and beautiful languages.