
文章转载自 开源中国社区 [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!")
变量和常量
var myVariable = 42
myVariable = 50
let myConstant 42
50
val myConstant 显式类型
let explicitDouble: Double = 70
val explicitDouble: Double = 70.0
强制类型转换
let label = "The wIDth is "
let wIDth 94
let wIDthLabel = label + String(wIDth)
val label "The wIDth is "
val wIDth 94
val wIDthLabel + wIDth
字符串插值
let apples 3
let oranges 5
let fruitSummary = "I have (apples + oranges) " +
"pIEces of fruit."
val apples 3
val oranges 5
val fruitSummary "I have ${apples + oranges} " 范围 *** 作符
let names = ["Anna", "Alex""Brian""Jack"]
let count names.count
for i in 0..
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)
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
for (index in 1..5) "$index times 5 is ${index * 5}"数组
var shopPingList "catfish""water" "tulips""blue paint"]
shopPingList[] "bottle of water"
val shopPingList )
映射
var occupations [
"Malcolm": "Captain""Kaylee""Mechanic"]
occupations["Jayne""Public Relations"
val occupations = mutableMapOf(
"Malcolm" to "Kaylee" to "Mechanic"
)
空集合
let emptyArray [String]()
let emptyDictionary = [String: float]()
val emptyArray ()
val emptyMap = mapOf()
FUNCTIONS
函数
func greet(_ name: _ day) -> String {
return "Hello (name), today is (day)."
}
greet"Bob""Tuesday" fun greet()"Hello $name, today is $day." 元组返回 func getGasPrices() -> (Double{ return 3.593.693.79} data class GasPrices(val aval b val cDouble) fun getGasPrices() = GasPrices参数的变量数目(Variable Number Of Arguments) func sumOfnumbersInt...) Int { var sum 0 for number in numbers { sum += number } return sum } sumOf4259712 fun sumOf(vararg 0 for (number in numbers) // sumOf() can also be written in a shorter way: fun sumOf(vararg numbersInt) .sum函数类型 func makeIncrementerInt { func addOnenumber-> Int { return 1 + number } return addOne } let increment = makeIncrementer() increment7 fun makeIncrementer(): { val addOne = fun)} val increment // makeIncrementer can also be written in a shorter way: fun makeIncrementernumber let numbers [2019712.map { 3 * $0 } val numbers = listof* it 排序 var listof(1, 5, 3, 12, 2).sorted() 命名参数 func areawIDthheight{ return wIDth * height } areawIDth: 2height3 fun area= wIDth * height area(wIDth height // This is also possible with named arguments area) area(height wIDth CLASSES 声明 class func simpleDescriptionString { return "A shape with (numberOfSIDes) sIDes." } 0
fun simpleDescription=
"A shape with $numberOfSIDes sIDes."
用法
var shape = Shape()
shape.numberOfSIDes 7
var shapeDescription .simpleDescription子类
class namedShape var numberOfSIDesInt 0
let nameString
init{
self.name = name
}
func simpleDescription}
class Square: namedShape sIDeLengthDouble
init.sIDeLength = sIDeLength
super.initname)
self4
}
func areaDouble {
return sIDeLength * sIDeLength
}
overrIDe func simpleDescription"A square with sIDes of length " +
sIDeLength + "."
}
let test = Square5.2"square"test.area 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); Box-sizing: border-Box !important; word-wrap: break-word !important;">类型检查 var
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
模式匹配
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" val nb 42 when nb{ in 0..79 -> println) 10 ) in 11..99 100..999 ) else 类型向下转换 for current in someObjects { if let movIE = current as? MovIE { print"MovIE: '(movIE.name)', " + "dir. (movIE.director)") (current in someObjects(current is { println"MovIE: '${current.name}',224) !important;"> "dir. ${current.director}"协议 protocol nameable { func nameString } func fxT"name is " x.name()) interface nameable {
fun name()}
fun f扩展
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"
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"
●本文编号199,以后想阅读这篇文章直接输入199即可。
●输入m获取文章目录
Java编程
涵盖:程序人生、算法与数据结构、黑客技术与网络安全、大数据技术、前端开发、Java、Python、Web开发、安卓开发、iOS开发、C/C++、.NET、linux、数据库、运维等。
总结以上是内存溢出为你收集整理的Kotlin 就像Swift ?看看 Kotlin 与 Swift全部内容,希望文章能够帮你解决Kotlin 就像Swift ?看看 Kotlin 与 Swift所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)