Tag: kotlin可以为

否则,Kotlin检查两次null

我有一个variablesdatePurchased的项目,可以为null。 根据购买日期,我生成一个标签。 当我检查datePurchased是否为空,否则我仍然必须检查为空。 它说聪明的演员是不可能的,因为这是一个可变的财产。 以下是我迄今为止所尝试的: 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 […]

用于处理非空对象和非空字符串表示的Kotlin成语

我有一个空的属性(一个Java对象),知道如何将自己转换为一个字符串,如果这种表示不是空的,我想做一些事情。 在Java中,这看起来像: MyObject obj = … if (obj != null) { String representation = obj.toString(); if (!StringUtils.isBlank(representation)) { doSomethingWith(representation); } } 我试图find把这个转换成Kotlin的最习惯的方式,我有: with(obj?.toString()) { if (!isNullOrBlank()) { doSomethingWith(representation) } } 但是对于这样一个简单的操作,感觉还是太多了。 我有这样一种感觉,即let我,可以和我在一起when ,可以把它缩短到稍微短一些。 步骤是: 如果对象(A)不为空 如果对象(A)的字符串表示(B)不是空白的 (B) 我试过了: when(where?.toString()) { isNullOrBlank() -> builder.append(this) } 但(1)失败: Unresolved reference. None of the following candidates is applicable because […]