Kotlin用let判断空æ或非空

前言

Kotlin中使用?.let来处理对象非空时的逻辑,那如果为空又怎么办呢?

解决
1
2
3
4
5
str?.let {
//非空
} ?: let {
//为空
}

如果为空不需要写很多代码,比如只是打印以下,那可以这么写

1
2
3
str?.let {
//非空
} ?: println("23")