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
2
3
4
plant?.let {
it.height = 20
displayGrowStage(it)
}

Use a different Parameter other than it

1
2
3
4
plant?.let { plant ->
plant.height = 20
displayGrowStage(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
2
3
val plant = nullablePlant ?: return
plant.height = 20
displayGrowStage(plant)