gpt4 book ai didi

android - 否则阻止多个? Kotlin 中的表达

转载 作者:行者123 更新时间:2023-11-29 18:58:14 26 4
gpt4 key购买 nike

下面的代码我需要else block

一?.二?.三?.四?.let {
}//这里需要 else block

有什么表达式可以用这种情况吗?

最佳答案

我认为你的问题是 let 也可以返回一个值,所以第二个 run block 执行:

  • 如果原始值为null
  • 如果返回值为空(不是你想要的!)

为避免这种情况发生,您需要从 let block 返回 Unit:

one?.two?.three?.four?.let {
doStuff()
Unit
} ?: run {
doOtherStuff()
}

您也可以使用典型的 if 语句,而不使用 ?。:

one?.two?.three?.four.let {
// ^ no ?.
if (it == null) doStuff() else doOtherStuff()
}

关于android - 否则阻止多个? Kotlin 中的表达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49531998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com