After exploring the syntactic similarities between Kotlin and Swift, we now turn to their differences. These differences, from handling null safety to implementing classes and inheritance, not only define the identity of each language but also significantly influence mobile app development. In this article, we explore the key elements that set Kotlin and Swift apart and offer insights into how these shape mobile application development.

Null Safety

While Swift uses optional types, Kotlin employs its own system for null safety, requiring a strict distinction between nullable and non-nullable types.

Swift (optional):
var name: String? = nil
Kotlin (nullable):
var name: String? = null

Accessing Variables

Swift (optional):
var name: String? = "John"
print(name) // Outputs Optional("John")

// Safe access with Optional Binding
if let safeName = name {
    print(safeName) // Outputs "John"
}
Kotlin (nullable):
var name: String? = "John"
println(name) // Outputs "John"

// Safe access with Safe Call Operator
println(name?.length) // Outputs the length of "John" if name is not null

Method Calling

Swift:

Swift requires developers to explicitly mark with `?` when they wish to call a method on an optional value.

var name: String? = "John"
print(name?.uppercased()) // Outputs Optional("JOHN")
Kotlin:

Kotlin uses the Safe Call Operator `?.` to call methods on nullable types, avoiding NullPointer Exceptions.

var name: String? = "John"
println(name?.toUpperCase()) // Outputs "JOHN" if name is not null

Default Values with the Elvis Operator

Another important aspect of null safety is the ability to provide default values for potentially nullable expressions.

Swift:
var name: String? = nil
let safeName = name ?? "Guest"
print(safeName) // Outputs "Guest"
Kotlin:
var name: String? = null
val safeName = name ?: "Guest"
println(safeName) // Outputs "Guest"

Classes and Inheritance

In Swift, inheritance is enabled through classes, while Kotlin has a clear separation between classes and interfaces with specific rules for inheritance.

Swift:
class Animal {
    func makeSound() {
        // Implementation
    }
}
class Dog: Animal {
}
Kotlin:
open class Animal {
    open fun makeSound() {
        // Implementation
    }
}
class Dog : Animal() {
}

Data Structures

Kotlin and Swift use different syntax for declaring arrays and dictionaries.

Swift:
let numbers: [Int] = [1, 2, 3]
let dictionary: [String: Int] = ["one": 1, "two": 2]
Kotlin:
val numbers = listOf(1, 2, 3)
val dictionary = mapOf("one" to 1, "two" to 2)

Functional Programming

Swift and Kotlin both support functional programming concepts, but their approach and the availability of functions differ.

Swift:
let doubledNumbers = numbers.map { $0 * 2 }
Kotlin:
val doubledNumbers = numbers.map { it * 2 }

Extension Functions

Both languages support Extension Functions, but their syntax and implementation differ.

Swift:
extension String {
    func greet() -> String {
        return "Hello, \(self)"
    }
}
print("John".greet()) // "Hello, John"
Kotlin:
fun String.greet(): String {
    return "Hello, $this"
}
println("John".greet()) // "Hello,

    John"

Pattern Matching

Swift and Kotlin both offer concepts for Pattern Matching, but the syntax and capabilities vary.

Swift:
let someValue: Any = 5
switch someValue {
case let intValue as Int:
    print("Integer: \(intValue)")
default:
    print("Another Type")
}
Kotlin:
val someValue: Any = 5
when (someValue) {
    is Int -> println("Integer: $someValue")
    else -> println("Another Type")
}

Delegation

Both languages offer mechanisms for delegation, but the approaches differ.

Swift:
protocol SomeProtocol {
    func doSomething()
}
class SomeClass: SomeProtocol {
    func doSomething() {
        print("Doing something")
    }
}
Kotlin:
interface SomeInterface {
    fun doSomething()
}
class SomeClass : SomeInterface by SomeOtherClass()