Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

How to add two conditions using let in Android Kotlin

I want to use let to check two conditions Lets say if I had to use if then this is the condition

if (it.data != null && !it.data!!.name.isEmpty()) {}

How can I convert it to use let I know that to check null this is what we do

it.data?.let {}

but I don't know how to check the second part which is if a string is empty or not. Please keep in mind that I have to check that data is not null AND data.name is not empty

Any help will be highly appreciated

question from:https://stackoverflow.com/questions/65680167/how-to-add-two-conditions-using-let-in-android-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For your specific case you could use takeIf

it.data
   ?.takeIf { data ->  data.name.isNotEmpty() }
   ?.let { data -> ...}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...