SlideShare a Scribd company logo
1 of 46
Download to read offline
Swift and Kotlin
Paul Murphy and Andrzej Sitek

Tuesday, April 14, 2014
Speakers
• Paul Murphy
• Background: Java Server Side, Native Android and
iOS Development, Javascript
• Developed Apps for: Vodafone, MTN, Discover
Digital, Avoca Learning, NPN, TVTextbook
• Andrzej Sitek
• Background: Front-End JavaScript, Node.js, Mobile
Web and Native Android Development
• Developed Apps for: MTN, Discover Digital, vRide,
Vanstar, Egopolis, Mobile Nokaut.pl
Introduction
• Brief discussion of App development and the
motivation for Cross Platform Development
• The common language features of Swift and Kotlin
• Short code demonstration of Swift and Kotlin and a
Short IDE demo of AppCode and Android Studio
• Conclusions
• Questions
Android StudioXcode
*https://github.com/google/j2objc
Cross Platform App Tools
Interface Guidelines
Material Design
Simplified App Architecture
Application Application
Activities
Fragments
ViewControllers Services
Simplified App Architecture
Model and Business Service Layers
Application Application
Activities
Fragments
ViewControllers Services
Model and Business Service Layers
Android StudioAppCode
Model and Business Service LayersModel and Business Service Layers
What is Swift?
What is Swift?
What is Swift?
• "Objective-C without the C"
• Relatively new compiled programming language
for iOS and OS X. It was Introduced at Apple's
2014 WWDC
• It works well with Cocoa Touch, Foundation Classes
and integrates well with existing Objective-C code
• Developed by Apple and is very well supported
• Built with the LLVM compiler framework and uses
Objective-C runtime
Why Swift?
• Employs modern programming concepts
• Simplified syntax comparing to Objective-C
• Friendly syntax to Java, Javascript and C#
developers
• By default no pointers and unsafe accessors
• Retains key Objective-C concepts
• Can run together with C, C++ and Objective-C in a
single program
What features does Swift bring?
• It is a nice syntax which is easy to learn
• Strongly typed
• Functions are first-class objects
• Header files are not required anymore
• Error handling rather than exception handling
• Full support of Unicode in Strings
The drawbacks of Swift
• Relatively new - not many apps are written 100% in Swift yet
• The iOS APIs are written with legacy data types, e.g.
NSArray, NSDictionary - there is poor compatibility between
these and the in built Swift datatypes
• Swift is a strongly typed language which requires strict
control over the initialisation of variables and the use of nil.
This is a small challenge to get used to for existing
Objective-C, Javascript and Java developers.
• Experienced Objective-C developers are reluctant to take
Swift on board for major projects.
• A knowledge of Objective-C is still necessary when dealing
with existing iOS APIs and third party libraries.
What is Kotlin?
What is Kotlin?
What is Kotlin?
• “Statically typed programming language targeting the
JVM and JavaScript”
• 100% interoperable with Java
• Made by JetBrains (folks behind IntelliJ IDEA and, by
extension, Android Studio)
• Named after an island near Saint Petersburg (the
location of the development office behind the project)
• Introduced in 2011 (well before Swift!!!)
• Android support came in 2nd milestone release (M2)
Why Kotlin?
• Actively developed and maintained by JetBrains
• Designed to be light by excluding all the domain-
specific heft
• It has some of the core features missing in Java
• Easy to integrate with Eclipse, IntelliJ, Android
Studio (official JetBrains Kotlin plugin is available)
• Versatile, No performance impact, very small
runtime
• Similar syntax to Swift
What features does Kotlin bring?
• Named and optional arguments
• Lambdas
• Null and type safety
• Data objects
• Simple singletons (object notation)
• Traits (interfaces with default implementation)
• Extension functions
The drawbacks of Kotlin
• It will take time until best practices emerge for Kotlin
in the Android world
• Annotation processors are not supported (such as
Butterknife)
• For more effective size and method count you have
to enable ProGuard:

Original ProGuard
Java 55 KB 54 KB
Kotlin 396 KB 57 KB 

Original ProGuard
Java 24 11
Kotlin 6,063 53
Similar Language Features
• Variables and Constants
• Functions and Methods
• Classes and Instantiation
• Classes and Inheritance
• Protocols and Traits
• Enums
Similar Language Features
• Nil and Null Safety (optional variables)
• Any and AnyObject
• Type Checks and Casts
• Generics
• Extensions and Typealias
• Error Handling
Swift Kotlin
var name: String = "Paul Murphy"

var address: String = "Cork"



let MON: String = "Monday"

let TUE: String = "Tuesday"

let WED: String = "Wednesday"

let THU: String = "Thursday"

let FRI: String = "Friday"
var name: String = "Andrzej Sitek"

var address: String = "Cork"



val MON: String = "Monday"

val TUE: String = "Tuesday"

val WED: String = "Wednesday"

val THU: String = "Thursday"

val FRI: String = "Friday"
Variables and Constants
Swift Kotlin
class FastFood {



func createBurger(item: String, id: Int) -> Burger
{

let ingredientFactory = KCIngredientFactory()

var burger = BeefBurger(factory:
ingredientFactory)

return burger

}



func createPizza(item: String, _ id: Int) ->
Burger {

let ingredientFactory = KCIngredientFactory()

var burger = BeefBurger(factory:
ingredientFactory)

return burger

}



fun createOrders() {

var burger = createBurger("beef", id: 101)

var pizza = createPizza("pepperoni", 101)

}



}
class FastFood {



fun createBurger(item: String, id: Int): Burger {

val ingredientFactory = KCIngredientFactory()

var burger = BeefBurger(factory =
ingredientFactory)

return burger

}



fun createPizza(item: String, id: Int): Burger {

val ingredientFactory = KCIngredientFactory()

var burger = BeefBurger(factory =
ingredientFactory)

return burger

}



fun createOrders() {

var burger = createBurger("beef", id = 101)

var pizza = createPizza("pepperoni", 101)

}



}
Functions and Methods
Swift Kotlin
class Burger {



var id: Int

var name: String



init(id: Int, name: String) {

self.id = id

self.name = name

}



}


var burger = Burger(id: 101, name:"KC Burger")

class Burger {



var id: Int

var name: String



constructor(id: Int, name: String) {

this.id = id

this.name = name

}



}


var burger = Burger(id=101, name="KC Burger")
Classes and Instantiation
Swift Kotlin
class Burger {



var id: Int

var name: String



init(id: Int, name: String) {

self.id = id

self.name = name

}



}
class BeefBurger : Burger {

override init(id: Int, name: String) {

super.init(id: id, name: name)

}

}


var burger = BeefBurger(id: 101, name:"KC Burger")
open class Burger {



var id: Int

var name: String



constructor(id: Int, name: String) {

this.id = id

this.name = name

}



}
class BeefBurger: Burger {

constructor(id: Int, name: String): super(id, name)
{



}

}


var burger = BeefBurger(id=101, name="KC Burger")
Classes and Inheritance
Swift Kotlin
protocol IngredientFactory {



func createBeef() -> Beef



func createChicken() -> Chicken



func createFish() -> Fish



func createCheese() -> Cheese



func createToppings() -> Array<Topping>



func createSauces() -> Array<Sauce>



}
trait IngredientFactory {



fun createBeef(): Beef



fun createChicken(): Chicken



fun createFish(): Fish



fun createCheese(): Cheese



fun createToppings(): ArrayList<Topping>



fun createSauces(): ArrayList<Sauce>



}
Protocols and Traits
Swift Kotlin
class KCIngredientFactory: IngredientFactory {



func createBeef() -> Beef {

return CorkBeef()

}



func createChicken() -> Chicken {

return ChickenBreast()

}



func createFish() -> Fish {

return BatteredFish()

}



func createCheese() -> Cheese {

return Cheese.MatureCheddar

}



func createToppings() -> Array<Topping> {

var toppings = Array<Topping>()

toppings.add(Topping.Onions)

return toppings

}



func createSauces() -> Array<Sauce> {

var sauces = Array<Sauce>()

sauces.add(Sauce.Mayo)

sauces.add(Sauce.Ketchup)

return sauces

}



class KCIngredientFactory: IngredientFactory {



override fun createBeef(): Beef {

return CorkBeef()

}



override fun createChicken(): Chicken {

return ChickenBreast()

}



override fun createFish(): Fish {

return BatteredFish()

}



override fun createCheese(): Cheese {

return Cheese.MatureCheddar

}



override fun createToppings(): ArrayList<Topping> {

var toppings = ArrayList<Topping>()

toppings.add(Topping.Onions)

return toppings

}



fun createSauces(): Array<Sauce> {

var sauces = ArrayList<Sauce>()

sauces.add(Sauce.Mayo)

sauces.add(Sauce.Ketchup)

return sauces

}



Protocols and Traits
Swift Kotlin
enum Cheese {

case MatureCheddar

case MeltyCheese

}



enum Topping {

case IcebergLettuce

case Gherkins

case Pickles

case Onions

}



enum Sauce {

case Mayo

case Ketchup

case Mustard

}
enum class Cheese {

MatureCheddar

MeltyCheese

}



enum class Topping {

IcebergLettuce

Gherkins

Pickles

Onions

}



enum class Sauce {

Mayo

Ketchup

Mustard

}
Enums
The Billion Dollar Mistake
Tony Hoare
I call it my billion-dollar mistake. It was the invention of the null reference in
1965…..But I couldn't resist the temptation to put in a null reference, simply
because it was so easy to implement. This has led to innumerable errors,
vulnerabilities, and system crashes, which have probably caused a billion
dollars of pain and damage in the last forty years.
Swift Kotlin
var a: String = “abc”
a = nil // compilation error
var b: String? = “abc”
b = nil // ok
class KCBurgerStand: BurgerStand {

let factory = KCIngredientFactory()

func createBurger(item: String) -> Burger {

var burger: Burger?

if (item == "beef") {

burger = BeefBurger(factory: factory)

burger!.name = "KC Special Burger"

} else if (item == "chicken") {

burger = ChickenBurger(factory: factory)

burger!.name = "KC Breast In A Bun"

} else if (item == "fish") {

burger = FishBurger(factory: factory)

burger!.name = "KC Atlantis Fish Burger"

}

return burger!

}

}
var a: String = “abc”
a = null // compilation error
var b: String? = “abc”
b = null // ok
class KCBurgerStand: BurgerStand {

val factory = KCIngredientFactory()

fun createBurger(item: String): Burger {

var burger: Burger? = null

if (item == "beef") {

burger = BeefBurger(factory: factory)

burger!!.name = "KC Special Burger"

} else if (item == "chicken") {

burger = ChickenBurger(factory: factory)

burger!!.name = "KC Creole In A Bun"

} else if (item == "fish") {

burger = FishBurger(factory: factory)

burger!!.name = "KC Atlantis Fish Burger"

}

return burger!!

}

}
Nil and Null Safety
Swift Kotlin
var anything : Any



var object : AnyObject

var anything : Any



var listOfObjects: List<AnyObject>

var listOfAnything: List<Any>
Important for interacting with existing
Objective-C APIs
Objective-C id maps to Swift AnyObject
id -> AnyObject




var object : Any



var listOfObjects: ArrayList<Any>
Any and AnyObject
Swift Kotlin
let beefBurger = BeefBurger()

let chickenBurger = ChickenBurger()

let fishBurger = FishBurger()

let burgers =
[beefBurger, chickenBurger, fishBurger]



for item in burgers {

if item is BeefBurger {

// downcast

let burger = item as BeefBurger

burger.prepare()

}

}
Uses is keyword
Uses as keyword
Type Checks and Casts
Swift Kotlin
func genericFunction<T>(item: T) -> T {



}
Note: AnyObject V Generics
• Strongly Typed
• Flexible
fun genericFunction<T>(item: T): T {



}
Generics
Swift Kotlin
extension String {

func toString() -> String {

return "summary"



}

}

extension Array {

mutating func add(item:T) {

append(item)

}

}
typealias Integer = Int

typealias HashMap = NSMutableDictionary

typealias ArrayList = NSMutableArray
fun String.description(): String {

return "summary"

}
Extensions and Typealias
Swift
enum ServerResponse {

case Result(String, String)

case Error(String)

}



let success = ServerResponse.Result("CheeseBurger", "Paul Murphy")

let failure = ServerResponse.Error("Out of cheese.")



switch success {

case let .Result(item, customerName):

let serverResponse = "Sunrise is at (item) for (customerName)."

case let .Error(error):

let serverResponse = "Failure... (error)"

}
Error Handling
Ignored Language Features
Feature Swift Kotlin
Class Import NO YES
Packages NO OPTIONAL
Exceptions NO YES
Visibility Modifiers YES YES
Primary Constructors NO YES
Annotations NO YES
Tuples YES NO
Property Observers YES NO
Operator Overloading YES YES
Data Classes NO YES
Type Safe Builders NO YES
Returns and Jumps NO YES
Language Cheatsheet
Swift Kotlin
nil null
self this
protocol trait
init constructor
func fun
-> :
let val
AnyObject Any
! !!
Example Model and Service Classes
Summary
• Cross Platform Tools add a layer of complexity and
dependency on third parties that can be easily
avoided
• Native App development on iOS and Android has
traditionally been different
• Swift and Kotlin share enough similarities in
language design and syntax to consider being
used in parallel for model and business app layers
• The use of a shared and similar IDE platform eases
parallel development in both languages
References
• Swift: https://developer.apple.com/swift/
• Kotlin: http://kotlinlang.org
• Kotlin web demos: http://kotlin-demo.jetbrains.com/
• Kotlin on Twitter: https://twitter.com/project_kotlin
Questions?

More Related Content

What's hot

Kotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language designKotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language designAndrey Breslav
 
Kotlin Slides from Devoxx 2011
Kotlin Slides from Devoxx 2011Kotlin Slides from Devoxx 2011
Kotlin Slides from Devoxx 2011Andrey Breslav
 
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...eMan s.r.o.
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin XPeppers
 
JVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinJVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinAndrey Breslav
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvmArnaud Giuliani
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Atif AbbAsi
 
Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Arnaud Giuliani
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017Hardik Trivedi
 
Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivitynklmish
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Arnaud Giuliani
 
Building microservices with Kotlin
Building microservices with KotlinBuilding microservices with Kotlin
Building microservices with KotlinHaim Yadid
 
Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Andrey Breslav
 
Coding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinCoding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinKai Koenig
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersBartosz Kosarzycki
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersBartosz Kosarzycki
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoMuhammad Abdullah
 
Kotlin cheat sheet by ekito
Kotlin cheat sheet by ekitoKotlin cheat sheet by ekito
Kotlin cheat sheet by ekitoArnaud Giuliani
 

What's hot (20)

Kotlin Overview
Kotlin OverviewKotlin Overview
Kotlin Overview
 
Kotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language designKotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language design
 
Kotlin Slides from Devoxx 2011
Kotlin Slides from Devoxx 2011Kotlin Slides from Devoxx 2011
Kotlin Slides from Devoxx 2011
 
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
 
JVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinJVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in Kotlin
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I
 
Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
 
Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivity
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
 
Building microservices with Kotlin
Building microservices with KotlinBuilding microservices with Kotlin
Building microservices with Kotlin
 
Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?
 
Coding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinCoding for Android on steroids with Kotlin
Coding for Android on steroids with Kotlin
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
Kotlin cheat sheet by ekito
Kotlin cheat sheet by ekitoKotlin cheat sheet by ekito
Kotlin cheat sheet by ekito
 

Viewers also liked

Kotlin gets Reflection
Kotlin gets ReflectionKotlin gets Reflection
Kotlin gets ReflectionAndrey Breslav
 
Kotlin for Android: Brief and Clear
Kotlin for Android: Brief and ClearKotlin for Android: Brief and Clear
Kotlin for Android: Brief and ClearAndrey Breslav
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation MobileAcademy
 
Flexible Types in Kotlin - JVMLS 2015
Flexible Types in Kotlin - JVMLS 2015Flexible Types in Kotlin - JVMLS 2015
Flexible Types in Kotlin - JVMLS 2015Andrey Breslav
 
Introduction to Kotlin: Brief and clear
Introduction to Kotlin: Brief and clearIntroduction to Kotlin: Brief and clear
Introduction to Kotlin: Brief and clearAndrey Breslav
 

Viewers also liked (7)

Kotlin gets Reflection
Kotlin gets ReflectionKotlin gets Reflection
Kotlin gets Reflection
 
Functions and data
Functions and dataFunctions and data
Functions and data
 
Kotlin for Android: Brief and Clear
Kotlin for Android: Brief and ClearKotlin for Android: Brief and Clear
Kotlin for Android: Brief and Clear
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
 
Flexible Types in Kotlin - JVMLS 2015
Flexible Types in Kotlin - JVMLS 2015Flexible Types in Kotlin - JVMLS 2015
Flexible Types in Kotlin - JVMLS 2015
 
Introduction to Kotlin: Brief and clear
Introduction to Kotlin: Brief and clearIntroduction to Kotlin: Brief and clear
Introduction to Kotlin: Brief and clear
 

Similar to Swift and Kotlin Presentation

Kotlin for android 2019
Kotlin for android 2019Kotlin for android 2019
Kotlin for android 2019Shady Selim
 
Launch Arguments & NSUserDefaults by Franck Lefebvre
Launch Arguments & NSUserDefaults by Franck LefebvreLaunch Arguments & NSUserDefaults by Franck Lefebvre
Launch Arguments & NSUserDefaults by Franck LefebvreCocoaHeads France
 
Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Mohamed Nabil, MSc.
 
iPhone Development Intro
iPhone Development IntroiPhone Development Intro
iPhone Development IntroLuis Azevedo
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWJest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWHolger Grosse-Plankermann
 
Frederick web meetup slides
Frederick web meetup slidesFrederick web meetup slides
Frederick web meetup slidesPat Zearfoss
 
Ci of js and apex using jasmine, phantom js and drone io df14
Ci of js and apex using jasmine, phantom js and drone io   df14Ci of js and apex using jasmine, phantom js and drone io   df14
Ci of js and apex using jasmine, phantom js and drone io df14Kevin Poorman
 
Everything-as-code - A polyglot adventure
Everything-as-code - A polyglot adventureEverything-as-code - A polyglot adventure
Everything-as-code - A polyglot adventureQAware GmbH
 
Everything-as-code. A polyglot adventure. #DevoxxPL
Everything-as-code. A polyglot adventure. #DevoxxPLEverything-as-code. A polyglot adventure. #DevoxxPL
Everything-as-code. A polyglot adventure. #DevoxxPLMario-Leander Reimer
 
Kotlin fundamentals - By: Ipan Ardian
Kotlin fundamentals - By: Ipan ArdianKotlin fundamentals - By: Ipan Ardian
Kotlin fundamentals - By: Ipan ArdianRizal Khilman
 
Kotlin Fundamentals
Kotlin Fundamentals Kotlin Fundamentals
Kotlin Fundamentals Ipan Ardian
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side SwiftChad Moone
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFXHendrik Ebbers
 
"Architecting and testing large iOS apps: lessons from Facebook". Adam Ernst,...
"Architecting and testing large iOS apps: lessons from Facebook". Adam Ernst,..."Architecting and testing large iOS apps: lessons from Facebook". Adam Ernst,...
"Architecting and testing large iOS apps: lessons from Facebook". Adam Ernst,...Yandex
 
Mobile Development integration tests
Mobile Development integration testsMobile Development integration tests
Mobile Development integration testsKenneth Poon
 
NativeScript: Cross-Platform Mobile Apps with JavaScript and Angular
NativeScript: Cross-Platform Mobile Apps with JavaScript and AngularNativeScript: Cross-Platform Mobile Apps with JavaScript and Angular
NativeScript: Cross-Platform Mobile Apps with JavaScript and AngularTodd Anglin
 
Sleipnir presentation
Sleipnir presentationSleipnir presentation
Sleipnir presentationatermenji
 

Similar to Swift and Kotlin Presentation (20)

Kotlin for android 2019
Kotlin for android 2019Kotlin for android 2019
Kotlin for android 2019
 
Launch Arguments & NSUserDefaults by Franck Lefebvre
Launch Arguments & NSUserDefaults by Franck LefebvreLaunch Arguments & NSUserDefaults by Franck Lefebvre
Launch Arguments & NSUserDefaults by Franck Lefebvre
 
Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1
 
iPhone Development Intro
iPhone Development IntroiPhone Development Intro
iPhone Development Intro
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWJest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRW
 
Titanium #MDS13
Titanium #MDS13Titanium #MDS13
Titanium #MDS13
 
Frederick web meetup slides
Frederick web meetup slidesFrederick web meetup slides
Frederick web meetup slides
 
Ci of js and apex using jasmine, phantom js and drone io df14
Ci of js and apex using jasmine, phantom js and drone io   df14Ci of js and apex using jasmine, phantom js and drone io   df14
Ci of js and apex using jasmine, phantom js and drone io df14
 
Everything-as-code - A polyglot adventure
Everything-as-code - A polyglot adventureEverything-as-code - A polyglot adventure
Everything-as-code - A polyglot adventure
 
Everything-as-code. A polyglot adventure. #DevoxxPL
Everything-as-code. A polyglot adventure. #DevoxxPLEverything-as-code. A polyglot adventure. #DevoxxPL
Everything-as-code. A polyglot adventure. #DevoxxPL
 
Kotlin fundamentals - By: Ipan Ardian
Kotlin fundamentals - By: Ipan ArdianKotlin fundamentals - By: Ipan Ardian
Kotlin fundamentals - By: Ipan Ardian
 
Kotlin Fundamentals
Kotlin Fundamentals Kotlin Fundamentals
Kotlin Fundamentals
 
CDI In Real Life
CDI In Real LifeCDI In Real Life
CDI In Real Life
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side Swift
 
Hello to Kotlin
Hello to KotlinHello to Kotlin
Hello to Kotlin
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
"Architecting and testing large iOS apps: lessons from Facebook". Adam Ernst,...
"Architecting and testing large iOS apps: lessons from Facebook". Adam Ernst,..."Architecting and testing large iOS apps: lessons from Facebook". Adam Ernst,...
"Architecting and testing large iOS apps: lessons from Facebook". Adam Ernst,...
 
Mobile Development integration tests
Mobile Development integration testsMobile Development integration tests
Mobile Development integration tests
 
NativeScript: Cross-Platform Mobile Apps with JavaScript and Angular
NativeScript: Cross-Platform Mobile Apps with JavaScript and AngularNativeScript: Cross-Platform Mobile Apps with JavaScript and Angular
NativeScript: Cross-Platform Mobile Apps with JavaScript and Angular
 
Sleipnir presentation
Sleipnir presentationSleipnir presentation
Sleipnir presentation
 

Swift and Kotlin Presentation

  • 1. Swift and Kotlin Paul Murphy and Andrzej Sitek
 Tuesday, April 14, 2014
  • 2. Speakers • Paul Murphy • Background: Java Server Side, Native Android and iOS Development, Javascript • Developed Apps for: Vodafone, MTN, Discover Digital, Avoca Learning, NPN, TVTextbook • Andrzej Sitek • Background: Front-End JavaScript, Node.js, Mobile Web and Native Android Development • Developed Apps for: MTN, Discover Digital, vRide, Vanstar, Egopolis, Mobile Nokaut.pl
  • 3. Introduction • Brief discussion of App development and the motivation for Cross Platform Development • The common language features of Swift and Kotlin • Short code demonstration of Swift and Kotlin and a Short IDE demo of AppCode and Android Studio • Conclusions • Questions
  • 7. Simplified App Architecture Application Application Activities Fragments ViewControllers Services
  • 8. Simplified App Architecture Model and Business Service Layers Application Application Activities Fragments ViewControllers Services Model and Business Service Layers
  • 10. Model and Business Service LayersModel and Business Service Layers
  • 13. What is Swift? • "Objective-C without the C" • Relatively new compiled programming language for iOS and OS X. It was Introduced at Apple's 2014 WWDC • It works well with Cocoa Touch, Foundation Classes and integrates well with existing Objective-C code • Developed by Apple and is very well supported • Built with the LLVM compiler framework and uses Objective-C runtime
  • 14. Why Swift? • Employs modern programming concepts • Simplified syntax comparing to Objective-C • Friendly syntax to Java, Javascript and C# developers • By default no pointers and unsafe accessors • Retains key Objective-C concepts • Can run together with C, C++ and Objective-C in a single program
  • 15. What features does Swift bring? • It is a nice syntax which is easy to learn • Strongly typed • Functions are first-class objects • Header files are not required anymore • Error handling rather than exception handling • Full support of Unicode in Strings
  • 16. The drawbacks of Swift • Relatively new - not many apps are written 100% in Swift yet • The iOS APIs are written with legacy data types, e.g. NSArray, NSDictionary - there is poor compatibility between these and the in built Swift datatypes • Swift is a strongly typed language which requires strict control over the initialisation of variables and the use of nil. This is a small challenge to get used to for existing Objective-C, Javascript and Java developers. • Experienced Objective-C developers are reluctant to take Swift on board for major projects. • A knowledge of Objective-C is still necessary when dealing with existing iOS APIs and third party libraries.
  • 19. What is Kotlin? • “Statically typed programming language targeting the JVM and JavaScript” • 100% interoperable with Java • Made by JetBrains (folks behind IntelliJ IDEA and, by extension, Android Studio) • Named after an island near Saint Petersburg (the location of the development office behind the project) • Introduced in 2011 (well before Swift!!!) • Android support came in 2nd milestone release (M2)
  • 20. Why Kotlin? • Actively developed and maintained by JetBrains • Designed to be light by excluding all the domain- specific heft • It has some of the core features missing in Java • Easy to integrate with Eclipse, IntelliJ, Android Studio (official JetBrains Kotlin plugin is available) • Versatile, No performance impact, very small runtime • Similar syntax to Swift
  • 21.
  • 22. What features does Kotlin bring? • Named and optional arguments • Lambdas • Null and type safety • Data objects • Simple singletons (object notation) • Traits (interfaces with default implementation) • Extension functions
  • 23. The drawbacks of Kotlin • It will take time until best practices emerge for Kotlin in the Android world • Annotation processors are not supported (such as Butterknife) • For more effective size and method count you have to enable ProGuard:
 Original ProGuard Java 55 KB 54 KB Kotlin 396 KB 57 KB 
 Original ProGuard Java 24 11 Kotlin 6,063 53
  • 24.
  • 25. Similar Language Features • Variables and Constants • Functions and Methods • Classes and Instantiation • Classes and Inheritance • Protocols and Traits • Enums
  • 26. Similar Language Features • Nil and Null Safety (optional variables) • Any and AnyObject • Type Checks and Casts • Generics • Extensions and Typealias • Error Handling
  • 27. Swift Kotlin var name: String = "Paul Murphy"
 var address: String = "Cork"
 
 let MON: String = "Monday"
 let TUE: String = "Tuesday"
 let WED: String = "Wednesday"
 let THU: String = "Thursday"
 let FRI: String = "Friday" var name: String = "Andrzej Sitek"
 var address: String = "Cork"
 
 val MON: String = "Monday"
 val TUE: String = "Tuesday"
 val WED: String = "Wednesday"
 val THU: String = "Thursday"
 val FRI: String = "Friday" Variables and Constants
  • 28. Swift Kotlin class FastFood {
 
 func createBurger(item: String, id: Int) -> Burger {
 let ingredientFactory = KCIngredientFactory()
 var burger = BeefBurger(factory: ingredientFactory)
 return burger
 }
 
 func createPizza(item: String, _ id: Int) -> Burger {
 let ingredientFactory = KCIngredientFactory()
 var burger = BeefBurger(factory: ingredientFactory)
 return burger
 }
 
 fun createOrders() {
 var burger = createBurger("beef", id: 101)
 var pizza = createPizza("pepperoni", 101)
 }
 
 } class FastFood {
 
 fun createBurger(item: String, id: Int): Burger {
 val ingredientFactory = KCIngredientFactory()
 var burger = BeefBurger(factory = ingredientFactory)
 return burger
 }
 
 fun createPizza(item: String, id: Int): Burger {
 val ingredientFactory = KCIngredientFactory()
 var burger = BeefBurger(factory = ingredientFactory)
 return burger
 }
 
 fun createOrders() {
 var burger = createBurger("beef", id = 101)
 var pizza = createPizza("pepperoni", 101)
 }
 
 } Functions and Methods
  • 29. Swift Kotlin class Burger {
 
 var id: Int
 var name: String
 
 init(id: Int, name: String) {
 self.id = id
 self.name = name
 }
 
 } 
 var burger = Burger(id: 101, name:"KC Burger")
 class Burger {
 
 var id: Int
 var name: String
 
 constructor(id: Int, name: String) {
 this.id = id
 this.name = name
 }
 
 } 
 var burger = Burger(id=101, name="KC Burger") Classes and Instantiation
  • 30. Swift Kotlin class Burger {
 
 var id: Int
 var name: String
 
 init(id: Int, name: String) {
 self.id = id
 self.name = name
 }
 
 } class BeefBurger : Burger {
 override init(id: Int, name: String) {
 super.init(id: id, name: name)
 }
 } 
 var burger = BeefBurger(id: 101, name:"KC Burger") open class Burger {
 
 var id: Int
 var name: String
 
 constructor(id: Int, name: String) {
 this.id = id
 this.name = name
 }
 
 } class BeefBurger: Burger {
 constructor(id: Int, name: String): super(id, name) {
 
 }
 } 
 var burger = BeefBurger(id=101, name="KC Burger") Classes and Inheritance
  • 31. Swift Kotlin protocol IngredientFactory {
 
 func createBeef() -> Beef
 
 func createChicken() -> Chicken
 
 func createFish() -> Fish
 
 func createCheese() -> Cheese
 
 func createToppings() -> Array<Topping>
 
 func createSauces() -> Array<Sauce>
 
 } trait IngredientFactory {
 
 fun createBeef(): Beef
 
 fun createChicken(): Chicken
 
 fun createFish(): Fish
 
 fun createCheese(): Cheese
 
 fun createToppings(): ArrayList<Topping>
 
 fun createSauces(): ArrayList<Sauce>
 
 } Protocols and Traits
  • 32. Swift Kotlin class KCIngredientFactory: IngredientFactory {
 
 func createBeef() -> Beef {
 return CorkBeef()
 }
 
 func createChicken() -> Chicken {
 return ChickenBreast()
 }
 
 func createFish() -> Fish {
 return BatteredFish()
 }
 
 func createCheese() -> Cheese {
 return Cheese.MatureCheddar
 }
 
 func createToppings() -> Array<Topping> {
 var toppings = Array<Topping>()
 toppings.add(Topping.Onions)
 return toppings
 }
 
 func createSauces() -> Array<Sauce> {
 var sauces = Array<Sauce>()
 sauces.add(Sauce.Mayo)
 sauces.add(Sauce.Ketchup)
 return sauces
 }
 
 class KCIngredientFactory: IngredientFactory {
 
 override fun createBeef(): Beef {
 return CorkBeef()
 }
 
 override fun createChicken(): Chicken {
 return ChickenBreast()
 }
 
 override fun createFish(): Fish {
 return BatteredFish()
 }
 
 override fun createCheese(): Cheese {
 return Cheese.MatureCheddar
 }
 
 override fun createToppings(): ArrayList<Topping> {
 var toppings = ArrayList<Topping>()
 toppings.add(Topping.Onions)
 return toppings
 }
 
 fun createSauces(): Array<Sauce> {
 var sauces = ArrayList<Sauce>()
 sauces.add(Sauce.Mayo)
 sauces.add(Sauce.Ketchup)
 return sauces
 }
 
 Protocols and Traits
  • 33. Swift Kotlin enum Cheese {
 case MatureCheddar
 case MeltyCheese
 }
 
 enum Topping {
 case IcebergLettuce
 case Gherkins
 case Pickles
 case Onions
 }
 
 enum Sauce {
 case Mayo
 case Ketchup
 case Mustard
 } enum class Cheese {
 MatureCheddar
 MeltyCheese
 }
 
 enum class Topping {
 IcebergLettuce
 Gherkins
 Pickles
 Onions
 }
 
 enum class Sauce {
 Mayo
 Ketchup
 Mustard
 } Enums
  • 34. The Billion Dollar Mistake Tony Hoare I call it my billion-dollar mistake. It was the invention of the null reference in 1965…..But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
  • 35. Swift Kotlin var a: String = “abc” a = nil // compilation error var b: String? = “abc” b = nil // ok class KCBurgerStand: BurgerStand {
 let factory = KCIngredientFactory()
 func createBurger(item: String) -> Burger {
 var burger: Burger?
 if (item == "beef") {
 burger = BeefBurger(factory: factory)
 burger!.name = "KC Special Burger"
 } else if (item == "chicken") {
 burger = ChickenBurger(factory: factory)
 burger!.name = "KC Breast In A Bun"
 } else if (item == "fish") {
 burger = FishBurger(factory: factory)
 burger!.name = "KC Atlantis Fish Burger"
 }
 return burger!
 }
 } var a: String = “abc” a = null // compilation error var b: String? = “abc” b = null // ok class KCBurgerStand: BurgerStand {
 val factory = KCIngredientFactory()
 fun createBurger(item: String): Burger {
 var burger: Burger? = null
 if (item == "beef") {
 burger = BeefBurger(factory: factory)
 burger!!.name = "KC Special Burger"
 } else if (item == "chicken") {
 burger = ChickenBurger(factory: factory)
 burger!!.name = "KC Creole In A Bun"
 } else if (item == "fish") {
 burger = FishBurger(factory: factory)
 burger!!.name = "KC Atlantis Fish Burger"
 }
 return burger!!
 }
 } Nil and Null Safety
  • 36. Swift Kotlin var anything : Any
 
 var object : AnyObject
 var anything : Any
 
 var listOfObjects: List<AnyObject>
 var listOfAnything: List<Any> Important for interacting with existing Objective-C APIs Objective-C id maps to Swift AnyObject id -> AnyObject 
 
 var object : Any
 
 var listOfObjects: ArrayList<Any> Any and AnyObject
  • 37. Swift Kotlin let beefBurger = BeefBurger()
 let chickenBurger = ChickenBurger()
 let fishBurger = FishBurger()
 let burgers = [beefBurger, chickenBurger, fishBurger]
 
 for item in burgers {
 if item is BeefBurger {
 // downcast
 let burger = item as BeefBurger
 burger.prepare()
 }
 } Uses is keyword Uses as keyword Type Checks and Casts
  • 38. Swift Kotlin func genericFunction<T>(item: T) -> T {
 
 } Note: AnyObject V Generics • Strongly Typed • Flexible fun genericFunction<T>(item: T): T {
 
 } Generics
  • 39. Swift Kotlin extension String {
 func toString() -> String {
 return "summary"
 
 }
 }
 extension Array {
 mutating func add(item:T) {
 append(item)
 }
 } typealias Integer = Int
 typealias HashMap = NSMutableDictionary
 typealias ArrayList = NSMutableArray fun String.description(): String {
 return "summary"
 } Extensions and Typealias
  • 40. Swift enum ServerResponse {
 case Result(String, String)
 case Error(String)
 }
 
 let success = ServerResponse.Result("CheeseBurger", "Paul Murphy")
 let failure = ServerResponse.Error("Out of cheese.")
 
 switch success {
 case let .Result(item, customerName):
 let serverResponse = "Sunrise is at (item) for (customerName)."
 case let .Error(error):
 let serverResponse = "Failure... (error)"
 } Error Handling
  • 41. Ignored Language Features Feature Swift Kotlin Class Import NO YES Packages NO OPTIONAL Exceptions NO YES Visibility Modifiers YES YES Primary Constructors NO YES Annotations NO YES Tuples YES NO Property Observers YES NO Operator Overloading YES YES Data Classes NO YES Type Safe Builders NO YES Returns and Jumps NO YES
  • 42. Language Cheatsheet Swift Kotlin nil null self this protocol trait init constructor func fun -> : let val AnyObject Any ! !!
  • 43. Example Model and Service Classes
  • 44. Summary • Cross Platform Tools add a layer of complexity and dependency on third parties that can be easily avoided • Native App development on iOS and Android has traditionally been different • Swift and Kotlin share enough similarities in language design and syntax to consider being used in parallel for model and business app layers • The use of a shared and similar IDE platform eases parallel development in both languages
  • 45. References • Swift: https://developer.apple.com/swift/ • Kotlin: http://kotlinlang.org • Kotlin web demos: http://kotlin-demo.jetbrains.com/ • Kotlin on Twitter: https://twitter.com/project_kotlin