[SwiftPH + PADC Meetup - May 2019] Swift Kotlin Syntax Comparison by Mark Kevin King
About the speaker:
Mark Kevin King (https://twitter.com/swiftymarks) is a guy who now works for himself and have partnerships with different companies. Born as an iOS developer but also does Android Development and Rails API. Workaholic? Yes! Trying hard to be a Sage developer. He has been developing mobile apps for more than 4 years now. One proud work to be released soon in the market is the Krispy Kreme App. Yum!
About SwiftPH:
SwiftPH and iOSCodersPH are the Apple Developer Communities in the Philippines. SwiftPH is responsible for our meetups and the iOSCodersPH is our slack group. Our community aims to bring all the iOS (and Apple) developer in the Philippines together to learn from one another and help our Apple developer ecosystem grow.
If you are interested, join us at https://meetup.com/swiftph. You can also reach out to us at swiftphofficial@gmail.com.
About Philippine Android Developer Community (PADC):
Philippine Android Developer Community is the Android developer community in the Philippines. (https://www.meetup.com/Philippine-Android-Developers-Community)
About the event:
Date: May 18, 2019
Venue: BeautyMNL, Philippines
More info here:
https://www.meetup.com/SwiftPH/events/261166657/
2. Outline
• History
• Basics
• Playgrounds vs. Main function
• Declaring functions
• Single-expression function
• Variable declarations
• Nullable
• String interpolations
3. Swift Language (History)
• Introduced in 2014
• Upgraded to v1.2 in 2014
• Swift 2
• Swift 2.2 (Open Source)
• Swift 3 - 2016
• Swift 4 - 2017
• Swift 5 - Now
4. Kotlin Language (History)
• Introduced by Jetbrains 2011
• Kotlin v1.0 - Feb 2016
• 2017 - First class support for Kotlin in Android
• Kotlin v1.2 - Sharing code between JVM and Javascript
platforms
• Kotlin v1.3 - October 2018
• Google I/0 2019 - Preferred language for Android App
Developers
14. If Else (Single If)
Kotlin
// Traditional Usage
var max = a
if (a < b) max = b
Swift
// Traditional Usage
var max = a
if a < b {
max = b
}
15. If Else (Single If else)
Kotlin
var max: Int
if (a > b) {
max = b
} else {
max = b
}
Swift
var max = a
if a < b {
max = b
} else {
max = b
}
16. If Else
Kotlin
var max: Int
if (a > b) {
max = b
} else {
max = b
}
Swift
var max = a
if a < b {
max = b
} else {
max = b
}
17. If Else
Kotlin
var max: Int
if (a > b) {
max = b
} else {
max = b
}
Swift
var max = a
if a < b {
max = b
} else {
max = b
}
18. Fast Iterations
Kotlin
val names = arrayOf(“Mike”, “Matt”, “Kurt”, “Steffi”)
for (name in names) {
print(“Hello $name”)
}
Swift
let names = [“Mike”, “Matt”, “Kurt”, “Steffi”]
for name in names {
print(“Hello (name”))
}
19. For Loops
Kotlin
for (i in 1..3) {
println(i)
}
Swift
for i in 1…10 {
print(i)
}
Swift
for i in 1..<10 {
print(i)
}
20. While Loops
Kotlin
while (condition) {
statements
}
Swift
while condition {
statements
}
Swift
while x < 10 {
x = x + 1
}
Kotlin
while (x < 10) {
x = x + 1
}
31. Constructor (Swift)
class Dog {
let name: String
let kind: String
init(name: String, kind: String) {
self.name = name
self.kind = kind
}
}
32. Inheritance
Swift
class Dog: Pet {
let name: String
let kind: String
init(name: String, kind:
String) {
self.name = name
self.kind = kind
}
}
Kotlin
class Dog: Pet {
constructor(name:String){
super.name = name
}
}
33. Lazy Variables
Swift
class Person {
let firstName: String
let lastName: String
var fullName: String {
return “(firstName) + (lastName)”
}
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
}
class Person(_firstname: String,
_lastname:String) {
var firstName: String
var lastName: String
init{
firstName = _firstname
lastName = _lastname
}
val fullName: String by lazy{
"$firstName $lastName"
37. Protocols or Interfaces
Swift
protocol Animal {
var canFly: Bool {get}
func eat()
func makeSound()
}
Kotlin
interface Person {
var canFly: Boolean
fun eat()
fun makeSound()
}