Swift 'if let' statement equivalent in Kotlin

前端 未结 14 1965
心在旅途
心在旅途 2020-12-02 07:32

In Kotlin is there an equivalent to the Swift code below?

if let a = b.val {

} else {

}
14条回答
  •  没有蜡笔的小新
    2020-12-02 08:12

    there are two answers above, both got a lot acceptances:

    1. str?.let{ } ?: run { }
    2. str?.also{ } ?: run { }

    Both seem to work in most of the usages, but #1 would fail in the following test:

    #2 seems better.

提交回复
热议问题