gpt4 book ai didi

kotlin - 我的情况下IF的减少数量

转载 作者:行者123 更新时间:2023-12-02 13:38:12 24 4
gpt4 key购买 nike

我的方法中这样的IF太多:

if (myObject?.name !=null)
first.text = myObject.name.bigThing

if (myObject?.age !=null)
second.text = myObject.age.bigThing

if (myObject?.surname !=null)
third.text = myObject.surname.bigThing

还有20个...

如何缩短代码?
age/surname/name是使用 Bigid: Int输入我自己的类 bigThing: String

最佳答案

一种方法是:

myObject?.age?.let { second.text = it.bigThing }

如果要将值放在 TextView中:
first.text = myObject?.age?.bigThing

关于kotlin - 我的情况下IF的减少数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51486857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com