Swift <-> Kotlin Syntax Cheatsheet

 

Rob Busack

Rob Busack is a mobile developer with a deep passion for iOS and climbing mountains.

Updated Jan 5, 2021

My background is primarily iOS development, though I’ve been dabbling in the Android world in recent years.  Fortunately Swift and Kotlin are very similar in a lot of ways, many of the paradigms are the same, just with different syntax.  Here’s a handy cheat-sheet for anyone who is familiar with one language, and just needs to look up the syntax for doing the same thing in the other language.

Declare Variables

Swift
let foo = 42           // constant (inferred type Int)
var bar = 7 // mutable
var baz: Int = 1 // explicitly typed Int
var biff: Int? = nil // "Optional Int", can be an Int or nil
Kotlin
val foo = 42           // constant
var bar = 7 // mutable
var baz: Int = 1 // explicitly typed Int
var biff: Int? = null // "Nullable Int", can be an Int or null

Optionals/Nullables

Swift
var foo: String? = "something"
var bar: String? = nil
foo?.count // evaluates to 7, but type is Int? (an Optional Int)
foo!.count // evaluates to 7, and type is Int (not an optional)
bar?.count // evaluates to nil
bar!.count // this line crashes


// Best to unwrap things safely:
var baz: String? = /* we don't know */
if let numberOfCharacters = baz?.count {
// in here, numberOfCharacters has a value, and it's type is Int
} else {
// code in here gets executed if baz was nil
}


// Use a default if baz is nil
let biff = baz ?? "default value"
Kotlin
var foo: String? = "something"
var bar: String? = null
foo?.length // evaluates to 7, but type is Int? (an Optional Int)
foo!!.length // evaluates to 7, and type is Int (not an optional)
bar?.length // evaluates to null
bar!!.length // this line crashes


// Best to unwrap things safely:
var baz: String? = /* we don't know */
baz?.length?.let { numberOfCharacters ->
// in here, numberOfCharacters has a value, and it's type is Int
} ?: run {
// code in here gets executed if baz was nil
}


// Use a default if baz is nil (Kotlin calls this the "Elvis Operator")
val biff = baz ?: "default value"

Functions

Swift
// Swift function parameters can have two names, the first used as a label
// when the function is called, the second as the variable name inside.
// Underscore is a special value that allows the caller to skip the label.

func makeGreeting( _ interjection: String = "Hey", to nameOrNil: String?) -> String {
if let name = nameOrNil {
return "\(interjection), \(name)!"
} else {
return "\(interjection) there!"
}
}

// usage
makeGreeting("Hi", to: "Joe") // returns "Hi, Joe!"
makeGreeting(to: "Diane") // returns "Hey, Diane!"
makeGreeting("Hello", to: nil) // returns "Hello there!"
Kotlin
// Kotlin function parameters have one name, and the caller is free 
// to use that name as a label when calling it or leave it out.

fun makeGreeting(interjection: String = "Hey", name: String?) : String {
name?.let {
return "${interjection}, ${name}!"
} ?: run {
return "${interjection} there!"
}
}

// usage
makeGreeting("Hi", "Joe") // returns "Hi, Joe!"
makeGreeting(name = "Diane") // returns "Hey, Diane!"
makeGreeting("Hello", null) // returns "Hello there!"
makeGreeting(interjection = "Hello", name = null) // returns "Hello there!"

Arrays & Maps

Swift
let fibonacciArray = [ 0, 1, 1, 2, 3, 5, 8 ]   // immutable, with type inferred from initial value
var myStrArray: [String]? = nil // either a mutable array of Strings, or the entire thing is nil
var myStrArray2: [String?] = []() // a mutable array that can contain either strings or nil values


var capitalOf: [String: String] = [
"Washington": "Olympia",
"Oregon": "Salem",
"New York": "Albany",
"Pennsylvania": "Harrisburg"
]

print( capitalOf["New York"] ?? "key not found" ) // prints "Albany"
Kotlin
val fibonacciArray = listOf<Int>( 0, 1, 1, 2, 3, 5, 8 )
var myStrArray: List<String>? = null
var myStrArray2: List<String?> = mutableListOf<String?>()


var capitalOf: MutableMap<String, String> = mutableMapOf(
"Washington" to "Olympia",
"Oregon" to "Salem",
"New York" to "Albany",
"Pennsylvania" to "Harrisburg"
)

print(capitalOf["New York"] ?: "key not found") // prints "Albany"

Loops

Swift
let daysOfTheWeek = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
for day in daysOfTheWeek {
print(day)
}
Kotlin
val daysOfTheWeek = listOf("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
for (day in daysOfTheWeek) {
print(day)
}