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

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.

主站蜘蛛池模板: 辽宁省| 石狮市| 浏阳市| 韶山市| 通江县| 夹江县| 津市市| 泗洪县| 嫩江县| 宁武县| 静安区| 芜湖市| 南昌市| 潞西市| 武隆县| 元阳县| 来安县| 无为县| 三亚市| 武强县| 安阳市| 巩留县| 兴安县| 土默特左旗| 双鸭山市| 新密市| 桦甸市| 云南省| 滕州市| 柯坪县| 屏边| 焦作市| 思南县| 黄冈市| 普安县| 镇安县| 沁阳市| 驻马店市| 修水县| 钟山县| 乐业县|