-
3 votesanswersviews
Kotlin项目中使用的旧Java库中的空安全性
假设我在旧/遗留Java库中有特定代码: public class JavaClass { private String notNullString; private String nullableString; private String unannotatedString; public JavaClass(@NotNull String notNullSt... -
3 votesanswersviews
Kotlin:不能同时应用两个条件检查“返回体”功能样式
我有一个简单的功能检查,如下所示: fun parseValidBluetoothBrickedId(controllerId: String?): Boolean{ if(controllerId != null){ if(controllerId.startsWith(BLUETOOTH_NAME_PREFIX) && controllerId.leng... -
3 votesanswersviews
Kotlin中功能变量的空安全运算符
在我的Kotlin应用程序中,我有像这样的可空变量 private var myCallback : (() -> Unit)? = null 是否可以使用null安全运算符 ? 来调用它?这给了我一个编译错误 . myCallback?() 如果它不是空的话,我发现只有这种丑陋的方式 if(myCallback != null) myCallback!!() -
5 votesanswersviews
在Kotlin中是'a?.let {}?:run {}`惯用语?
我在S.O.看到了以下评论 . 发帖,我很好奇: 为什么不使用if进行空检查? a?.let {}?:run {}仅在极少数情况下适用,否则它不是惯用的 - voddan 2016年5月15日在7:29最好的方法来检查kotlin? 为什么构造"only appropriate in rare cases"?Kotlin的首席工程师说, run允许您在elvis运算符的... -
1 votesanswersviews
Spring(kotlin)控制器接收数据类的非可空参数为null
我面临一个意外的错误,我不知道如何处理 . 我有一个这样的数据类: data class Payload ( @SerializedName("id") var id: String @SerializedName("type") var type: String, @SerializedName("data") ... -
10 votesanswersviews
Kotlin不可空的值可以为null吗?
我有后端给我一些json . 我把它解析给我的 class : class SomeData( @SerializedName("user_name") val name: String, @SerializedName("user_city") val city: String, var notNullableValue: String) 使... -
74 votesanswersviews
什么是Kotlin双爆炸(!!)操作员?
我正在用Android Studio将Java转换为Kotlin . 我在实例变量后获得双重爆炸 . 什么是双重爆炸,更重要的是这在哪里记录? mMap!!.addMarker(MarkerOptions().position(london).title("Marker in London")) -
0 votesanswersviews
null to listOf(),在一个班轮中Kotlin中的listOf(value)不为null?
让 f() 返回一个可以为空的值 . 我想做的是如果 f() 为null,则获取一个空列表,否则如果 f() 不为null,则获取单个项目 value 的列表 . 在Scala中,我们可以这样做: Option(f()).toList 或者更详细 Option(f()).map(v => List(v)).getOrElse(List.empty) 在Kotlin中,没有 Option ... -
3 votesanswersviews
null检查后Kotlin 'unsafe call on nullable receiver'编译错误
这是错误的屏幕截图: 我正在使用Intellij v14.0.1 Kotlin插件v0.9.206 . 我的理解是,在空检查之后,编译器不应该向我显示错误 . 我错了吗? -
13 votesanswersviews
如何在Kotlin中对非空,非空字符串进行惯用测试?
我是Kotlin的新手,我正在寻找建议,以更优雅的方式重写以下代码 . var s: String? = "abc" if (s != null && s.isNotEmpty()) { // Do something } 如果我使用以下代码: if (s?.isNotEmpty()) { 编译器会抱怨 Required: Boolean Found... -
4 votesanswersviews
Kotlin对类属性的null安全性
如何避免将 !! 用于类的可选属性 class PostDetailsActivity { private var post: Post? = null fun test() { if (post != null) { postDetailsTitle.text = post.title // Error I have to sti... -
3 votesanswersviews
Kotlin对null的非null断言
由于Kotlin有非空的断言,我发现了一些有趣的东西...... val myvar: String = null!! 它会崩溃 . 但重点是,它不会在编译时检查 . 该应用程序将在运行时崩溃 . 它不应该抛出编译时错误吗? -
0 votesanswersviews
在Kotlin中该类外部的函数中处理类中字段的空值
class StuffContainer(var stuff: MutableList<String>?) { fun appendSomeStuff(newStuff: List<String>) { // Appends to stuff, initialising it if null stuff?.addAll(newStuff) ?: le... -
1 votesanswersviews
当param不可为空时,未为reified类型插入空检查
TL;DR 具有reified类型的函数在生成代码时是否应考虑类型参数的可为空性? Test case 考虑以下Kotlin代码;两种方法的唯一区别是类型绑定是否可为空( Any? )或不是( Any ) . @Test fun testNonNullableBound() { val x: Int = nonNullableBound() } @Test fun testNullabl...