kotlin && swift

变量关键字:

        kotlin: var     val

        swift:  var     let

字符串模板:

        kotlin: "这是一个字符串${插值}"

        swift:"这是一个字符串\(插值)"

创建数组:

        kotlin: 1、arrayOf(1, 2, 3)

                   2、Array(3) { i -> (i + 1) }

                  3、arrayOfNulls<T>(3)   长度为3 类型为T 默认元素为null的数组

                4、emptyArray<T>() 长度为0 类型为T 的空数组

                上面4种都是得到的Array类实例,除此外还有

                  ByteArray

                  CharArray

                  ShortArray

                  IntArray

                  LongArray

                  FloatArray

                  DoubleArray

                  BooleanArray八种数组类型,这8种数组类型与Array类型没有关系,都有自己的用法

             实例化方法都是两种xxxArrayOf()和构造函数,例如  intArrayOf(1, 2, 3)和IntArray(3){i+1}

swift: 使用[]创建数组和字典

        var fruits = ["strawberries", "limes", "tangerines"]

        fruits[1] = "grapes"

print(fruits) 输出 ["strawberries", "grapes", "tangerines"]

var occupations = [ "Malcolm": "Captain", "Kaylee": "Mechanic", ]

occupations["Jayne"] = "Public Relations"

print(occupations)

输出["Kaylee": "Mechanic", "Malcolm": "Captain", "Jayne": "Public Relations"]

您还可以使用括号来编写空数组或字典。对于数组,空数组为[],对于字典,空字典为 [:] 。

let emptyArray: [String] = []    //需要指定类型

let emptyDictionary: [String: Float] = [:]   //需要指定类型

流程控制

        kotlin:

//sampleStart
  var max = a
  if (a < b) max = b
// With else  
if (a > b) {
   max = a
} else { 
   max = b
}
// 作为表达式
max = if (a > b) a else b
if 表达式的分支可以是代码块,这种情况最后的表达式作为该块的值:
val max = if (a > b) {
    print("Choose a")
    a
} else {
    print("Choose b")
    b
}

for循环

for (item in collection) print(item)

swift:

let individualScores = [75, 43, 103, 87, 12]

var teamScore = 0

for score in individualScores {

if score > 50 {

        teamScore += 3

} else {

        teamScore += 1

}}

print(teamScore)// Prints "11"

if语句可结合let使用判断可空值是否为空,例如:

var cannull String? = "notnull"

if let v = cannull{ //当cannull 不为nil时,条件判断为true ,也可以用相同名称简写为 if let cannull{} 

        print(v)

}

kotlin中 ?: 对应swift中 ?? 

do-while

kotlin: 

        while (x > 0) { x-- }
        do {

                 val y = retrieveData()

        } while (y != null) // y 在此处可见

swift:

        var n = 2

        while n < 100 {

                n *= 2

                }

        print(n)// Prints "128"

        var m = 2

        repeat {

                m *= 2

        } while m < 100

区间

        kotlin:

                1.. 10                  // 1到10 包含10

                1 until 10          // 1到10 不包含10

        swift:

                1... 10                  // 1到10 包含10

                1..<10                 // 1到10 不包含10

函数

        kotlin:

                fun test(p:String):String{

                        return "result"

                }

        swift:

                func test(p:String)->String{

                        return "result"

                }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值