android - 赋值不是表达式,在这种情况下只允许使用表达式 - Kotlin
问题描述
我在将 java 转换为 kotlin 时遇到错误,无法理解如何解决这个特定错误。
internal fun getDiff(to: Calendar, from: Calendar): Long {
var diffInSeconds = (to.time.time - from.time.time) / 1000
val diff = longArrayOf(0, 0, 0, 0)
diff[3] = if (diffInSeconds >= 60) diffInSeconds % 60
else diffInSeconds // sec
diff[2] = if ((diffInSeconds = diffInSeconds / 60)>= 60)
diffInSeconds % 60
else
diffInSeconds // min
diff[1] = if ((diffInSeconds = diffInSeconds / 60) >= 24)
diffInSeconds % 24
else
diffInSeconds // hour
diff[0] = (diffInSeconds = diffInSeconds / 24) // day
Log.e("days", diff[0].toString() + "")
return diff[0]
}
以下行:(diffInSeconds = diffInSeconds / 60)
显示错误显示
赋值不是表达式,在此上下文中只允许使用表达式
解决方案
语法无效,因为diffInSeconds = diffInSeconds / 60
它不是 Kotlin 中的表达式。就这样做
var a = diffInSeconds /= 60
diff[1] = if (a >= 24)
推荐阅读
- oracle - 确定 Oracle 数据库中表/视图的最后使用日期
- algorithm - 带有 if-else 语句的 for 循环的大哦
- linq - MVC Linq .Random() 语法错误
- python - 使用python根据特定条件将csv文件划分为垃圾
- java - 如何解决异常类 java.util.HashMap$Values 无法转换为 HashMap 中的类 java.util.Enumeration
- python - 将升序序列号字段添加到集合中的所有现有 mongodb 文档
- firebase - Flutter:如何将firebase uid设置为用户文档ID?
- spring-boot - 如果在我们正在测试的方法中使用新关键字声明实例,我们可以模拟它吗?
- powershell - 将 powershell 导出到 CSV 的问题
- c - 在C中将结构添加到结构链表中