Swift 'if let' statement equivalent in Kotlin

前端 未结 14 1955
心在旅途
心在旅途 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:25

    My answer is totally a copy cat from the others. However, I cannot understand their expression easily. So I guess it would be nice to provide an more understandable answer.

    In swift:

    if let a = b.val {
      //use "a" as unwrapped
    }
    else {
    
    }
    

    In Kotlin:

    b.val?.let{a -> 
      //use "a" as unwrapped
    } ?: run{
      //else case
    }
    

提交回复
热议问题