我有一个带有变量的项目datePurchased
,它可以为空。根据购买日期,我生成一个标签。当我检查 datePurchased 是否为空时,在 else 分支中我仍然必须检查是否为空。它说智能转换是不可能的,因为它是一个可变属性。
这是我到目前为止所尝试的:
if (datePurchased == null) {
""
} else {
if (datePurchased.isToday()) {//error here
}
}
when {
datePurchased == null -> {
}
datePurchased.isToday() -> {//smart cast bla bla mutable bla bla
datePurchased?.isToday() -> {//expected Boolean, got Boolean?
datePurchased?.isToday()?:false -> {//all good, but does not look nice, since datePurchased can't be null here
}
else -> {
}
}