美文网首页
对比学习kotlin,swift,es(一)

对比学习kotlin,swift,es(一)

作者: Pyrrha_89c6 | 来源:发表于2017-12-02 23:42 被阅读0次

    模板字符串、forEach、Null Safety、常量,变量声明关键字

    • 模板字符串:
       kotlin : "Hello, ${args}" //kotlin用顶层对象可以简写为$arg,省略{}
       swift : "Hello, \(args)!" 
       es6 : `Hello, ${args}`  
    
    • forEach:
      • 遍历array
      kotlin:
        for (item in array)
          println("item : $item")
        for ((index, value) in array.withIndex()) { //带index遍历
          println("the element at $index is $value")
        }
      swift : 
        for item in array {
          print("item : \(item)")
        }
        for (index,item) in array.enumerated() {
          print("the element at \(index) is \(item)")
        }
      es6 ->  
        for (item of array )
          console.log(`item : ${item}`)
      
      • 遍历map
      kotlin:
           未知
      swift : 
        for (key, value) in map {
          print("item : \(key) - \(value)")
        }
      
      es6 ->  
        for (let [key, value] of phoneBookMap) 
          console.log(`item : $(key) - $(value)`)
      
    • Null Safety
      kotlin:
        var a: String = "abc"
        a = null //编译错误,一个常规的字符串无法赋值为空
        var b: String? = "abc" //正确在类型声明后面加一个?
        b = null // ok
        val l = a.length  //编译通过
        val l = b.length // 由于b可能是空,所以编译不通过
        //判空
        val l = if (b != null) b.length else -1 
            ==
        val l = b?.length ?: -1 //Elvis operator
        //Safe Calls
        b?.length //如果b不是空则返回b的值,否则返回null
        bob?.department?.head?.name //可链式调用,只要中间有一个为空就会返回空
        //可以用let关键字来执行非空时的代码
        val listWithNulls: List<String?> = listOf("A", null)
        for (item in listWithNulls) {
            item?.let { println(it) } // prints A and ignores null
        }
        // return 和 throw 也可用在 Elvis operator 右边
        fun foo(node: Node): String? {
          val parent = node.getParent() ?: return null
          val name = node.getName() ?: throw IllegalArgumentException("name expected")
          return null
        }
        //强制解包(应该少用)
        val l = b!!.length //和swift的强制解包相同,现在可以将l看做正常的String,若b为null程序会crash,抛出控制针
        //安全类型转换
        val aInt: Int? = a as? Int //若a不是Int型会返回null
      swift:
        var a: String = "abc"
        a = nil //编译错误,一个常规的字符串无法赋值为空
        var b: String? = "abc" //正确在类型声明后面加一个?
        b = nil // ok
        var l = a.count  //编译通过
        var l = b.count // 由于b可能是空,所以编译不通过
        解包:
          强制解包:
             var l = b!.length
          安全解包:
            if let i = b{  //非空时处理
              //use i to do something
            }
            func doSomething(str: String?){
              guard let v = str else { return } //处理空
             // use v to do something
            }
      es6: 未加入标准
      
    • 常量,变量声明关键字
      es6不建议用var声明变量,var声明变量不受{ //code } 作用域限制
    语言 常量 变量
    kotlin val var
    swift let var
    es6 const let

    相关文章

      网友评论

          本文标题:对比学习kotlin,swift,es(一)

          本文链接:https://www.haomeiwen.com/subject/hmiobxtx.html