Swift刚出来的时候学过一遍,2年了,Kotlin出来了,我看着和Swift相似。写写复习Swift学习Kotlin。
学习工具介绍:由于属于语法学习,本人推荐 Swift:在Xcode新建playground进行学习,直观迅速。 Kotlin:用官网上的在线编译工具即可. 传送门
下面是一个国外工程师总结的语法 参考至:http://nilhcem.com/swift-is-like-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..