搜索
您的当前位置:首页正文

Kotlin中问号(?)和两个叹号(!!)的含义

来源:二三娱乐

在刚开始用Kotlin写代码的时候经常因为!!和?不加报警告错误。
具体提示如下代码所示:

  protected var loadingDialog: Dialog? = null
  //java隐藏actionBar
   if (null != getActionBar()) {
        getActionBar().hide();
    }
   //Kotlin隐藏actionBar
   actionBar?.hide()  
   //Kotlin调用弹出框显示方法
   loadingDialog?.show()
   假如你不加!!或者?,会出现如下告警提示
    Smart cast to 'loadingDialog' is impossible, because 'loadingDialog' is a mutable property that could have been changed by this time

?:表示当前是否对象可以为空

!!: 通知编译器不做非空校验。如果运行时发现变量为空,就扔出异常

这两个都是Kotlin为我们提供的检验空指针的方法

Top