https://kotlinlang.org logo
#android
Title
# android
o

oscarg798

07/14/2017, 1:32 AM
myString?.let{ mTextview.text = myString }
v

vivekneel

07/14/2017, 4:25 AM
Yup. Anyways, did not you get any warning or compile time error when you called let on a nullable object?
m

marstran

07/14/2017, 7:06 AM
let
is defined for all objects. Even nullable ones.
j

joachim.reiss.

07/14/2017, 7:23 AM
you can also write `it`inside of the let
t

tokajip

07/14/2017, 7:53 AM
or you can use
apply
and it’ll as simple as
text=..
m

marstran

07/14/2017, 8:01 AM
Do you mean
mTextview.apply { text = myString }
@tokajip ? In that case, you lose the null-check on
myString
.
t

tokajip

07/14/2017, 8:20 AM
Yes, but I mean
mTextView?.apply { text = myString }
you’ll have null-check on
myString
and save the
myString
or
it
inside the function
m

marstran

07/14/2017, 8:30 AM
Now you have the nullcheck on
mTextView
which isn't nullable.
myString
can still be null.
t

tokajip

07/14/2017, 8:40 AM
Ohh, my mistake. I thought in the original post it was
TextView
on called
?.let{ }
but as I looked again, I saw its a
String
But on Android you can set null for a
TextView
text
property, because the system’ll transform it to an empty
String
👍 1
m

marstran

07/14/2017, 10:39 AM
Yep, but he might want to keep the old text when
myString
is null.
3 Views