加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 就像 Kotlin?看看 Swift 与 Kotlin

发布时间:2020-12-14 05:14:02 所属栏目:百科 来源:网络整理
导读:(点击 上方公众号 ,可快速关注) 英文:nilhcem.com 编译:开源中国 如有好文章投稿,请点击 → 这里了解详情 一位国外的程序员认为 Swift 的语法与 Kotlin 相似,并整理了一些 Swift 和 Kotlin 的对比,下面是一些例子,大家不妨也看看。 BASICS Hello Wo

(点击上方公众号,可快速关注)

英文:nilhcem.com

编译:开源中国

如有好文章投稿,请点击 → 这里了解详情


一位国外的程序员认为 Swift 的语法与 Kotlin 相似,并整理了一些 Swift 和 Kotlin 的对比,下面是一些例子,大家不妨也看看。


BASICS


Hello World


Swift


print("Hello,?world!")


Kotlin


println("Hello,?world!")?


变量和常量


Swift


var?myVariable?=?42

myVariable?=?50

let?myConstant?42


Kotlin


50

val?myConstant?42


显式类型


Swift


let?explicitDouble:?Double?=?70


Kotlin


val?explicitDouble:?Double?=?70.0


强制类型转换


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?+?width


字符串插值


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


let?names?=?["Anna",?"Alex""Brian""Jack"]

let?count?=?names.count

for?i?in?0..


Kotlin


val?names?=?arrayOf()

val?count?.count()

for?(i?in?0..count?-?1)?{

????println("Person?${i?+?1}?is?called?${names[i]}")

}

//?Person?1?is?called?Anna

//?Person?2?is?called?Alex

//?Person?3?is?called?Brian

//?Person?4?is?called?Jack


包罗广泛的范围操作符(Inclusive Range Operator)


Swift


for?index?in?1...5?{

????print"(index)?times?5?is?(index?*?5)"//?1?times?5?is?5

//?2?times?5?is?10

//?3?times?5?is?15

//?4?times?5?is?20

//?5?times?5?is?25


Kotlin


for?(index?in?1..5)?"$index?times?5?is?${index?*?5}"//?5?times?5?is?25


数组


Swift


var?shoppingList?"catfish""water"????"tulips""blue?paint"]

shoppingList[]?"bottle?of?water"


Kotlin


val?shoppingList?)

"bottle?of?water"


映射


Swift


var?occupations?[

????"Malcolm":?"Captain""Kaylee""Mechanic"]

occupations["Jayne""Public?Relations"


Kotlin


val?occupations?=?mutableMapOf(

????"Malcolm"?to?"Kaylee"?to?"Mechanic"

)

"Public?Relations"


空集合


Swift


let?emptyArray?[String]()

let?emptyDictionary?=?[String:?Float]()


Kotlin


val?emptyArray?()

val?emptyMap?=?mapOf()


FUNCTIONS


函数


Swift


func?greet(_?name:?_?day)?->?String?{

????return?"Hello?(name),?today?is?(day)."

}

greet"Bob""Tuesday")


Kotlin


fun?greet()"Hello?$name,?today?is?$day."

)


元组返回


Swift


func?getGasPrices()?->?(Double{

????return?(3.593.693.79}


Kotlin


data?class?GasPrices(val?aval?b?????val?cDouble)

fun?getGasPrices()?=?GasPrices参数的变量数目(Variable Number Of Arguments)


Swift


func?sumOfnumbersInt...)?Int?{

????var?sum?0

????for?number?in?numbers?{

????????sum?+=?number

????}

????return?sum

}

sumOf4259712)


Kotlin


fun?sumOf(vararg?0

????for?(number?in?numbers)

?

//?sumOf()?can?also?be?written?in?a?shorter?way:

fun?sumOf(vararg?numbersInt)?.sum函数类型


Swift


func?makeIncrementerInt?{

????func?addOnenumber->?Int?{

????????return?1?+?number

????}

????return?addOne

}

let?increment?=?makeIncrementer()

increment7)


Kotlin


fun?makeIncrementer():?{

????val?addOne?=?fun)}

val?increment?//?makeIncrementer?can?also?be?written?in?a?shorter?way:

fun?makeIncrementernumber


映射


Swift


let?numbers?[2019712.map?{?3?*?$0?}


Kotlin


val?numbers?=?listOf(*?it?}


排序


Swift


var?mutableArray?1532mutableArray.sort()


Kotlin


listOf(1,?5,?3,?12,?2).sorted()


命名参数


Swift


func?areawidthheight{

????return?width?*?height

}

areawidth:?2height3)


Kotlin


fun?area=?width?*?height

area(width?height?//?This?is?also?possible?with?named?arguments

area)

area(height?width?CLASSES


声明


Swift


class?Shape?var?numberOfSides?0

????func?simpleDescriptionString?{

????????return?"A?shape?with?(numberOfSides)?sides."

????}

}


Kotlin


0

????fun?simpleDescription=

????????"A?shape?with?$numberOfSides?sides."

}


用法


Swift


var?shape?=?Shape()

shape.numberOfSides?7

var?shapeDescription?.simpleDescription()


Kotlin


子类


Swift


class?NamedShape?var?numberOfSidesInt?0

????let?nameString

?

????init{

????????self.name?=?name

????}

?

????func?simpleDescription}

?

class?Square:?NamedShape?sideLengthDouble

?

????init.sideLength?=?sideLength

????????super.init:?name)

????????self4

????}

?

????func?areaDouble?{

????????return?sideLength?*?sideLength

????}

?

????override?func?simpleDescription"A?square?with?sides?of?length?"?+

???????sideLength?+?"."

????}

?

let?test?=?Square5.2"square"test.area()


Kotlin


open?class?NamedShape0

?

????open?fun?simpleDescriptionclass?Square(BigDecimal:

????????NamedShape{

????init?{

????????numberOfSides?}

?

????fun?areasideLength.pow)

?

????override?fun?simpleDescription"A?square?with?sides?of?length?$sideLength."

}

?

val?test?(BigDecimal"5.2"),66);">类型检查


Swift


var?movieCount?0

var?songCount?0

?

for?item?in?library?{

????if?item?is?Movie?{

????????movieCount?+=?1

????}?else?if?item?is?Song?{

????????songCount?0

?

for?(item?in?library{

????if?(item?is?Movie{

????????++movieCount

????}?else?if?Song++songCount

????模式匹配


Swift


let?nb?42

switch?nb?{

????case?0...789:?print"single?digit")

????case?10"double?digits"11...99100...999"triple?digits")

????default"four?or?more?digits"}


Kotlin


val?nb?42

when?nb{

????in?0..79?->?println)

????10?)

????in?11..99?100..999?)

????else?类型向下转换


Swift


for?current?in?someObjects?{

????if?let?movie?=?current?as??Movie?{

????????print"Movie:?'(movie.name)',?"?+

????????????"dir.?(movie.director)")

????}


Kotlin


for?(current?in?someObjects(current?is?{

????????println"Movie:?'${current.name}',224) !important;"> ????"dir.?${current.director}"协议


Swift


protocol?Nameable?{

????func?nameString

}

?

func?fxT"Name?is?"?x.name())

interface?Nameable?{

????fun?name()}

?

fun?f扩展


Swift


extension?Double?km{?return?self?*?1_000.0?}

????m{?return?self?cm/?100.0?mmft3.28084?}

let?oneInch?25.4.mm

print"One?inch?is?(oneInch)?meters")

//?prints?"One?inch?is?0.0254?meters"

let?threeFeet?3.ft

print"Three?feet?is?(threeFeet)?meters"//?prints?"Three?feet?is?0.914399970739201?meters"


Kotlin


val?.kmDouble?get=?this?*?1000

val?.Double?get=?this

val?=?this?100

val?3.28084

?

val?oneInch?25.4.mm

println"One?inch?is?$oneInch?meters"//?prints?"One?inch?is?0.0254?meters"

val?threeFeet?3.0.ft

println"Three?feet?is?$threeFeet?meters"//?prints?"Three?feet?is?0.914399970739201?meters"


看完本文有收获?请分享给更多人

关注「 iOS大全 」,提升iOS技能

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读