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:

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

They both support null safety using Optionals:

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

Swift:

The same with Kotlin:

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:

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

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:

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

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:

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

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

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:

Now the same with Kotlin:

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

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

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.

Thanks for the read, I hope you enjoyed it. If you have any questions or would like to know more about the differences and similarities between Swift and Kotlin, feel free to leave a comment!

You may also contact me on my company’s website or on Twitter.

Software Developer and Entrepreneur at productionbuild.de