Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin for Swift developers

Kotlin for Swift developers

Swift and Kotlin have a lot of concepts in common but they also have a few notable differences. Knowing both languages can help you write better code and share ideas between developers of “the other platform”. We will cover the basics of Kotlin before converting a Swift app into a Kotlin one. We will use concepts such as sealed classes, extensions and many others to make our code more robust and readable.

Accompanying blogpost: https://www.liip.ch/en/blog/app-builders-kotlin-swift-developers

Jonas Schmid

April 29, 2019
Tweet

More Decks by Jonas Schmid

Other Decks in Programming

Transcript

  1. Jonas Schmid • iOS / Android developer • I like

    both platforms and synergies between them
  2. This talk is in two parts • Differences between Swift

    and Kotlin • Code demo: conversion from Swift to Kotlin
  3. Variables and constants Swift var myVariable = 21 myVariable =

    42 let myConstant = 21 Kotlin var myVariable = 21 myVariable = 42 val myConstant = 21
  4. String interpolation in Swift let name = "Lugano" let message

    = "The name \(name) has \(name.count) letters" String templates in Kotlin val name = "Lugano" val message = "The name $name has ${name.length} letters"
  5. Functions Swift func showAlert(title: String, _ description: String? = nil)

    {} showAlert(title: "Hello Lugano!", "Second best city in Switzerland") showAlert(title: "Hello Lugano!") Kotlin fun showAlert(title: String, description: String? = null) {} showAlert("Hello Lugano!", "Second best city in Switzerland") showAlert(title = "Hello Lugano!")
  6. Classes: Swift class Person { var name: String? func greetings()

    -> String { return "Hello \(name ?? "stranger")" } } let person = Person() person.name = "Jonas" person.greetings()
  7. Classes: Kotlin class Person { var name: String? = null

    fun greetings() = "Hello ${name ?: "stranger"}" } val person = Person() person.name = "Jonas" person.greetings()
  8. Initializers: Swift class Car { var wheels: Int init(wheels: Int)

    { self.wheels = wheels } } let car = Car(wheels: 4) car.wheels
  9. Constructors: Kotlin class Car( var wheels: Int ) { //

    ... } val car = Car(4) car.wheels
  10. Value type: Swift var plane1 = Plane(wings: 2) var plane2

    = plane1 plane2.wings = 4 plane1.wings // 2
  11. Reference type: Kotlin var plane1 = Plane(2) var plane2 =

    plane1 plane2.wings = 4 plane1.wings // 4
  12. Enums: Swift enum ApiResponse { case success(value: [Todo]) case failure(error:

    String) } Sealed classes: Kotlin sealed class ApiResponse data class Success(val value: List<Todo>) : ApiResponse() data class Failure(val error: String) : ApiResponse()
  13. Pattern matching: Swift func handleResponse(_ response: ApiResponse<String>) { switch response

    { case .success(let value): print(value) case .failure(let error): os_log(error) } } let response = ApiResponse.success(value: "Hello") handleResponse(response)
  14. Pattern matching: Kotlin fun handleResponse(response: ApiResponse<String>) { when(response) { is

    Success -> println(response.value) is Failure -> println(response.error) } } val response = ApiResponse.Success("Hello") handleResponse(response)
  15. Type casting Let's set the base classes protocol Shape {

    func area() -> Double } struct Circle: Shape { var radius: Double func area() -> Double { return Double.pi * pow(radius, 2) } }
  16. Type casting: Swift func displayShape(_ shape: Shape) { if let

    circle = shape as? Circle { print("Circle radius: \(circle.radius)") } print("Shape area: \(shape.area())") } let circle = Circle(radius: 3) displayShape(circle)
  17. Smart casting: Kotlin fun displayShape(shape: Shape) { if (shape is

    Circle) { println("Circle radius: ${shape.radius}") } println("Shape area: ${shape.area()}") } val circle = Circle(3.0) displayShape(circle)
  18. guard: Swift func playWithShape(shape: Shape) { guard let circle =

    shape as? Circle else { return } print("Circle radius: \(circle.radius)") }
  19. return: Kotlin fun playWithShape(shape: Shape) { val circle = shape

    as? Circle ?: return println("Circle radius: ${circle.radius}") }
  20. Summary • Basics of Kotlin • Code conversion was fast

    • Swift and Kotlin are fairly similar • Lot of patterns from one can be used with the other • That was just a start