Slide 1

Slide 1 text

Hello

Slide 2

Slide 2 text

Who is a Swift developer ?

Slide 3

Slide 3 text

Who is a Kotlin developer ?

Slide 4

Slide 4 text

Welcome to Kotlin for Swift developers

Slide 5

Slide 5 text

Jonas Schmid • iOS / Android developer • I like both platforms and synergies between them

Slide 6

Slide 6 text

This talk is in two parts • Differences between Swift and Kotlin • Code demo: conversion from Swift to Kotlin

Slide 7

Slide 7 text

Buckle up We'll go fast

Slide 8

Slide 8 text

Let's go ! !

Slide 9

Slide 9 text

Variables and constants Swift var myVariable = 21 myVariable = 42 let myConstant = 21 Kotlin var myVariable = 21 myVariable = 42 val myConstant = 21

Slide 10

Slide 10 text

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"

Slide 11

Slide 11 text

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!")

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Classes: Kotlin class Person { var name: String? = null fun greetings() = "Hello ${name ?: "stranger"}" } val person = Person() person.name = "Jonas" person.greetings()

Slide 14

Slide 14 text

Initializers: Swift class Car { var wheels: Int init(wheels: Int) { self.wheels = wheels } } let car = Car(wheels: 4) car.wheels

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Structs: Swift struct Plane { var wings: Int } let plane = Plane(wings: 2)

Slide 17

Slide 17 text

Data classes: Kotlin data class Plane ( var wings: Int ) val plane = Plane(2)

Slide 18

Slide 18 text

Value type: Swift var plane1 = Plane(wings: 2) var plane2 = plane1 plane2.wings = 4 plane1.wings // 2

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Extensions Swift extension Plane { func fly() { ... } } Kotlin fun Plane.fly() { ... }

Slide 21

Slide 21 text

Enums: Swift enum ApiResponse { case success(value: [Todo]) case failure(error: String) } Sealed classes: Kotlin sealed class ApiResponse data class Success(val value: List) : ApiResponse() data class Failure(val error: String) : ApiResponse()

Slide 22

Slide 22 text

Pattern matching: Swift func handleResponse(_ response: ApiResponse) { switch response { case .success(let value): print(value) case .failure(let error): os_log(error) } } let response = ApiResponse.success(value: "Hello") handleResponse(response)

Slide 23

Slide 23 text

Pattern matching: Kotlin fun handleResponse(response: ApiResponse) { when(response) { is Success -> println(response.value) is Failure -> println(response.error) } } val response = ApiResponse.Success("Hello") handleResponse(response)

Slide 24

Slide 24 text

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) } }

Slide 25

Slide 25 text

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)

Slide 26

Slide 26 text

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)

Slide 27

Slide 27 text

guard: Swift func playWithShape(shape: Shape) { guard let circle = shape as? Circle else { return } print("Circle radius: \(circle.radius)") }

Slide 28

Slide 28 text

return: Kotlin fun playWithShape(shape: Shape) { val circle = shape as? Circle ?: return println("Circle radius: ${circle.radius}") }

Slide 29

Slide 29 text

DEMO

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Everyone can grow by learning the other language

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Jonas Schmid @jonas_schmid ✉ [email protected] See you at the App Builders party! Slides: liip.to/ab19