官术网_书友最值得收藏!

The let function

Another tool to deal with nullable variables is the let function. This is actually not an operator, nor a language special construct. It is a function defined in the Kotlin standard library. Let's see the syntax of let combined with the safe call operator:

    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
 
        savedInstanceState?.let { 
            println(it.getBoolean("isLocked")) // 1 
        } 
    } 
  1. savedInstanceState inside the let function can be accessed using the it variable.

As mentioned before, the right-hand expression of the safe call operator will be only evaluated if the left-hand side is not null. In this case, the right-hand side is a let function that takes another function (lambda) as a parameter. Code defined in the block after let will be executed if savedInstanceState is not null. We will learn more about it and how to define such functions in Chapter 7, Extension Functions and Properties.

主站蜘蛛池模板: 峨眉山市| 西藏| 镇宁| 铜梁县| 田阳县| 融水| 高雄市| 台北县| 阿坝县| 周至县| 华容县| 宜黄县| 剑河县| 三门峡市| 鄂伦春自治旗| 娱乐| 保山市| 芦溪县| 两当县| 康保县| 屏山县| 澜沧| 绍兴县| 六盘水市| 蕉岭县| 临汾市| 伊川县| 蒲城县| 左权县| 周宁县| 武定县| 民乐县| 务川| 崇信县| 克山县| 吉水县| 冷水江市| 闻喜县| 康保县| 巴彦淖尔市| 芜湖县|