Tag:

Kotlin编译器不能确定该variables在do-while循环中是不可空的

我有以下方法。 它的逻辑非常简单,如果设置了权限,那么在有值的时候调用左边(非空值)。 当我以下面的方式写它,它的工作。 fun goNext(from: Node): Node? { var prev : Node = from var next : Node? = from.right if (next != null) { prev = next next = next.left while (next != null) { prev = next next = next.left } } return prev } 相反,如果我尝试使用do-while循环缩短代码,那么它不再是在Node next智能代码。 它显示这个错误: Type mismatch. Required: Node Found: […]

否则,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 […]