文章转载自 开源中国社区
[http://www.oschina.net]
链接:
www.oschina.net/news/85013/swift-is-like-kotlin
(点击尾部阅读原文前往)
原文:http://nilhcem.com/swift-is-like-kotlin/
一位国外的程序员认为 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
var
myVariable
=
42
myVariable
=
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
=
label
+
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
(
"Anna"
,
"Alex"
,
"Brian"
,
"Jack"
)
val
count
=
names
.
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
)
{
println
(
"$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
BASICS
数组
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
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
let
emptyArray
=
[
String
]()
let
emptyDictionary
=
[
String
:
Float
]()
Kotlin
val
emptyArray
=
arrayOf
()
val
emptyMap
=
mapOf
()
FUNCTIONS
函数
Swift
func
greet
(
_
name
:
String
,
_
day
:
String
)
->
String
{
return
"Hello (name), today is (day)."
}
greet
(
"Bob"
,
"Tuesday"
)
Kotlin
fun
greet
(
name
:
String
,
day
:
String
)
:
String
{
return
"Hello $name, today is $day."
}
greet
(
"Bob"
,
"Tuesday"
)
元组返回
Swift
func
getGasPrices
()
->
(
Double
,
Double
,
Double
)
{
return
(
3.59
,
3.69
,
3.79
)
}
Kotlin
data
class
GasPrices
(
val
a
:
Double
,
val
b
:
Double
,
val
c
:
Double
)
fun
getGasPrices
()
=
GasPrices
(
3.59
,
3.69
,
3.79
)
参数的变量数目(Variable Number Of Arguments)
Swift
func
sumOf
(
_
numbers
:
Int
...)
->
Int
{
var
sum
=
0
for
number
in
numbers
{
sum
+=
number
}
return
sum
}
sumOf
(
42
,
597
,
12
)
Kotlin
fun
sumOf
(
vararg
numbers
:
Int
)
:
Int
{
var
sum
=
0
for
(
number
in
numbers
)
{
sum
+=
number
}
return
sum
}
sumOf
(
42
,
597
,
12
)
// sumOf() can also be written in a shorter way:
fun
sumOf
(
vararg
numbers
:
Int
)
=
numbers
.
sum
()
函数类型
Swift
func
makeIncrementer
()
->
(
Int
->
Int
)
{
func
addOne
(
number
:
Int
)
->
Int
{
return
1
+
number
}
return
addOne
}
let
increment
=
makeIncrementer
()
increment
(
7
)
Kotlin
fun
makeIncrementer
()
:
(
Int
)
->
Int
{
val
addOne
=
fun
(
number
:
Int
)
:
Int
{
return
1
+
number
}
return
addOne
}
val
increment
=
makeIncrementer
()
increment
(
7
)
// makeIncrementer can also be written in a shorter way:
fun
makeIncrementer
()
=
fun
(
number
:
Int
)
=
1
+
number
映射
Swift
let
numbers
=
[
20
,
19
,
7
,
12
]
numbers
.
map
{
3
*
$
0
}
Kotlin
val
numbers
=
listOf
(
20
,
19
,
7
,
12
)
numbers
.
map
{
3
*
it
}
排序
Swift
var
mutableArray
=
[
1
,
5
,
3
,
12
,
2
]
mutableArray
.
sort
()
Kotlin
listOf(1, 5, 3, 12, 2).sorted()
命名参数
Swift
func
area
(
width
:
Int
,
height
:
Int
)
->
Int
{
return
width
*
height
}
area
(
width
:
2
,
height
:
3
)
Kotlin
fun
area
(
width
:
Int
,
height
:
Int
)
=
width
*
height
area