Swift is like Kotlin

来源:互联网 发布:js 对象排序 编辑:程序博客网 时间:2024/06/14 01:52
**BASICS**

Hello World
Swift

print(“Hello, world!”)

Kotlin

println(“Hello, world!”)

**Variables And Constants**Swift

var myVariable = 42
myVariable = 50
let myConstant = 42

Kotlin

var myVariable = 42
myVariable = 50
val myConstant = 42

**Explicit Types**Swift

let explicitDouble: Double = 70

Kotlin

val explicitDouble: Double = 70.0

**Type Coercion**Swift

let label = “The width is ”
let width = 94
let widthLabel = label + String(width)

Kotlin

val label = “The width is ”
val width = 94
val widthLabel = label + width

**String Interpolation**Swift

let apples = 3
let oranges = 5
let fruitSummary = “I have (apples + oranges) ” +
“pieces of fruit.”

Kotlin

val apples = 3
val oranges = 5
val fruitSummary = “I have ${apples + oranges} ” +
“pieces of fruit.”

**Range Operator**Swift

let names = [“Anna”, “Alex”, “Brian”, “Jack”]
let count = names.count
for i in 0..