Kotlin Unwrap Optional Variable
Jun 25, 2019
Here is how you can unwrap optional variable in Kotlin
Let / Also
You can use a lambda function let / also to safe unwrap the optional variable.
(Assume plant is the optional variable)
1 | plant?.let { |
Use a different Parameter other than it
1 | plant?.let { plant -> |
Return If Null
In Swift, you have guard let xxx = xxx else to unwrap the optional variable. So here is the similar thing I will do in Kotlin.
1 | val plant = nullablePlant ?: return |
