A Comparison of Swift and Kotlin Languages
This article focuses on the main similarities and differences between Swift and Kotlin, including implementation, style, syntax and other important details. By Aaqib Hussain.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
A Comparison of Swift and Kotlin Languages
30 mins
- Understanding Similarities Between Swift and Kotlin
- Declaring Properties
- Kotlin — Mutable Properties
- Swift – Mutable Properties
- Kotlin — Immutable Properties
- Swift — Immutable Properties
- Data Structures
- Arrays
- Dictionaries / Maps
- Functions
- Lambda Functions / Closures
- Nullable / Optional Types
- Handling a Nullable / Optional Type
- Control Flow
- Classes
- Class Extensions
- Interface / Protocol
- Functional Programming Tools
- Understanding Differences Between Swift and Kotlin
- Where to Go From Here?
Understanding Differences Between Swift and Kotlin
As you have seen, Swift and Kotlin are similar in many ways. However, they still have some differences.
Data Class vs. Struct
Swift has value types, such as structs, as well as reference types, while Kotlin only has reference types. Take a look at some examples.
Kotlin — Data Class
Consider the Person class from the previous examples in Kotlin:
val personOne = Person("Jon", "12 August")
val personTwo = personOne // assign to another variable
personTwo.name = "Vanessa"
println(personOne.name) // prints Vanessa
println(personTwo.name) // prints Vanessa
Swift — Struct
Now, consider the above Person class as a Swift struct
.
struct Person {
let name: String
let dob: String
}
let personOne = Person(name: "Jon", dob: "12 August")
var personTwo = personOne // assign to another variable
personTwo.name = "Vanessa"
print(personOne.name) // prints Jon
print(personTwo.name) // prints Vanessa
val vs. let
Other than not being able to re-assign the value, val
and let
do have a small difference in how they are handled across the two languages.
Kotlin — val
Consider the following persons list:
val persons = mutableListOf(Person("Jon", "12 August"),
Person("Kim", "10 July"),
Person("Vanessa", "12 August"),
Person("Alisa", "26 March"))
persons.add(Person("Shaun", "11 Jan")) // can append a new value
Swift - let
In Swift:
let persons = [Person(name: "Jon", dob: "12 August"),
Person(name: "Kim", dob: "10 July"),
Person(name: "Vanessa", dob: "12 August"),
Person(name: "Alisa", dob: "26 March")]
persons.append(Person(name: "Shaun", dob: "11 Jan")) // throws compile time error
The fundamental difference here is in the treatment of an array or list. Kotlin lets you append values to a mutable list even when it's declared with a val
. Similarly, you can declare an array in Kotlin using val
and afterward re-assign members of the array using subscript notation. In contrast, in Swift, an array must be declared as mutable using var
in order to have members updated with subscript notation or to append values to it.
Where to Go From Here?
After reading this comparison between Swift and Kotlin, you must have gotten a pretty good idea of how much they have in common.
Both of these languages are not limited to mobile development, either. They both are used for server side development and cross-platform app development as well. To serve this purpose there are a number of frameworks out there to help you with it. Vapor and Kitura are used for server side Swift and video courses on these can be found on our website. For Kotlin there is Spring Boot. For cross-platform app development there are frameworks like Kotlin-Native and Scade.
Finally, here are some additional resources to help you learn about the similarities and differences between Swift and Kotlin:
- Swift Is Like Kotlin
- Kotlin vs. Swift: Are Android and iOS Moving Towards Creating a Universal Language?
- Swift vs. Kotlin
- Swift Vs. Kotlin: Similarities and Differences Every Developer Should Know
- Server Side Swift with Vapor
- Kotlin Apprentice
- Swift Apprentice
- My Transition From Swift to Kotlin by Hector Matos (KotlinConf 2017)
If you have any questions or comments, please let us know in the discussion below!