背景:在公司flutter 开发已经近两年了,在flutter过程中经常需要编写插件,涉及到IOS端,想着一些简单逻辑的插件就自己写了,不需要劳烦ios同学了。之前虽然有玩过ios的demo,现在基本又忘了,自己观察了下,现在的高级语言语法已经愈发接近了。swift与传统oc差别巨大,却和我们安卓的kotlin语言甚是相似。
我们先来看看语法,这里主要拿来与kotlin对比
变量和常量
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小括号
字典/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"