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

  • Android Development with Kotlin
  • Marcin Moskala Igor Wojda
  • 147字
  • 2021-07-02 18:48:35

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.

主站蜘蛛池模板: 昭通市| 西城区| 玉门市| 武义县| 太谷县| 北辰区| 玛沁县| 汝南县| 高碑店市| 白朗县| 墨玉县| 成武县| 彩票| 定结县| 通山县| 玉田县| 浙江省| 平果县| 泰来县| 独山县| 深州市| 进贤县| 来安县| 清丰县| 湘潭市| 海南省| 平山县| 凉城县| 元朗区| 台安县| 斗六市| 雅安市| 许昌县| 石家庄市| 肥乡县| 龙州县| 辽阳县| 刚察县| 南丹县| 科技| 吴旗县|