Kotlin/Swift语法对比

发布时间 2023-08-02 15:07:20作者: cps666

原文地址 juejin.cn

变量和常量

Swift

var myVariable = 42

let myConstant = 42


Kotlin

var myVariable = 42

val myConstant = 42


对比: 变量都是 variable,常量 kotlin 是 val,swift 是 let,let 在 js 中主要是做局部变量,js 中常量有 const

类型声明

Swift

let explicitDouble: Double = 70


Kotlin

val explicitDouble: Double = 70.0


对比:类型的声明都是冒号

字符串插值

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."


对比:Swift 使用 “\” 符号 Kotlin 使用 “$” 符号

数组

Swift

var shoppingList = ["catfish", "water","tulips", "blue paint"]
shoppingList[1] = "bottle of water"


Kotlin

val shoppingList = arrayOf("catfish", "water",
"tulips", "blue paint")
shoppingList[1] = "bottle of water"


对比:swift 是中括号,和 dart 一致,kotlin用listOf()方法

字典 / map

Swift

var occupations = ["Malcolm": "Captain","Kaylee": "Mechanic",]
occupations["Jayne"] = "Public Relations"


Kotlin

val occupations = mutableMapOf(
"Malcolm" to "Captain",
"Kaylee" to "Mechanic"
)
occupations["Jayne"] = "Public Relations"


对比:Swift 使用 ":" 连接 key value 。Kotlin 可用 “to” 关键字 连接 key value

函数定义

Swift

func greet(_ name: String, day: String) -> String {
    return "Hello \(name), today is \(day)."
    }
greet("", day: "String");


Kotlin

fun greet(name: String = "name", day: String= "sunday"): String {
return "Hello $name, today is $day."
}
greet("Bob", "Tuesday")


对比: 1.kotlin 方法定义 fun , swift 是 func 2.Kotlin 返回值是冒号,swift 是 -> 。 3.swift 用_代表忽略参数标签,不然都需要 key-value 形式一样去传参。 kotlin 只有带有默认值的参数才需要带参数名 4. 两者都有默认参数,高阶函数。swift 返回值可以是元组。

Swift

class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}


Kotlin

class Shape {
    var numberOfSides = 0
    fun simpleDescription() =
        "A shape with $numberOfSides sides."
}


对象实例化

Swift

var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
Kotlin
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()


子类继承

Swift

class NamedShape {
    var numberOfSides: Int = 0
    let name: String

    init(name: String) {
        self.name = name
    }

    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

class Square: NamedShape {
    var sideLength: Double

    init(sideLength: Double, name: String) {
        self.sideLength = sideLength
        super.init(name: name)
        self.numberOfSides = 4
    }

    func area() -> Double {
        return sideLength * sideLength
    }

    override func simpleDescription() -> String {
        return "A square with sides of length " +
           sideLength + "."
    }
}

let test = Square(sideLength: 5.2, name: "square")
test.area()
test.simpleDescription()


Kotlin Kotlin 类继承,需在父类用 open 关键字显式声明

open class NamedShape(val name: String) {
    var numberOfSides = 0

    open fun simpleDescription() =
        "A shape with $numberOfSides sides."
}

class Square(var sideLength: BigDecimal, name: String) :
        NamedShape(name) {
    init {
        numberOfSides = 4
    }

    fun area() = sideLength.pow(2)

    override fun simpleDescription() =
        "A square with sides of length $sideLength."
}

val test = Square(BigDecimal("5.2"), "square")
test.area()
test.simpleDescription()


OC/swift 的协议 对标 java 和 kotlin 的接口

Swift Swift 使用 protocol 关键字

protocol Nameable {
    func name() -> String
}

func f<T: Nameable>(x: T) {
    print("Name is " + x.name())
}
Kotlin
Kotlin使用 interface 关键字

interface Nameable {
    fun name(): String
}

fun f<T: Nameable>(x: T) {
    println("Name is " + x.name())
}


扩展

Swift Swift 需使用 extension 关键字

extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// 输出 "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// 输出 "Three feet is 0.914399970739201 meters"


Kotlin Kotlin 则直接使用 “.” 符号

val Double.km: Double get() = this * 1000
val Double.m: Double get() = this
val Double.cm: Double get() = this / 100
val Double.mm: Double get() = this / 1000
val Double.ft: Double get() = this / 3.28084

val oneInch = 25.4.mm
println("One inch is $oneInch meters")
// 输出 "One inch is 0.0254 meters"
val threeFeet = 3.0.ft
println("Three feet is $threeFeet meters")
// 输出 "Three feet is 0.914399970739201 meters"


这里主要列出了一些日常常用的一些语法,可见 kotlin 和 swfit 差别并不大,基本上看一下差别就是可以直接上手的。当然还有很多东西是需要深入了解的,线程相关的 kotlin 协程和 swift 的 GCD 等等。