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

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.

主站蜘蛛池模板: 巴南区| 石阡县| 达拉特旗| 静宁县| 乌审旗| 田林县| 康平县| 新建县| 江永县| 三台县| 富民县| 肃宁县| 望奎县| 绥化市| 盐源县| 罗甸县| 仁布县| 吴忠市| 蒲城县| 威海市| 南乐县| 建始县| 商水县| 潍坊市| 莲花县| 广西| 阿瓦提县| 昌都县| 陆川县| 崇仁县| 新竹市| 兰考县| 宣化县| 双柏县| 社会| 泸溪县| 五原县| 株洲市| 连南| 呼玛县| 炎陵县|