- 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 } }
- 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.
推薦閱讀
- Spring Cloud Alibaba微服務架構設計與開發實戰
- 編寫高質量代碼:改善Python程序的91個建議
- x86匯編語言:從實模式到保護模式(第2版)
- iOS應用逆向工程(第2版)
- Python忍者秘籍
- iOS開發實戰:從入門到上架App Store(第2版) (移動開發叢書)
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- C編程技巧:117個問題解決方案示例
- 算法圖解
- Exploring SE for Android
- Deep Learning for Natural Language Processing
- iOS Development with Xamarin Cookbook
- 產品架構評估原理與方法
- 秒懂算法:用常識解讀數據結構與算法
- Java Web程序開發參考手冊