Tag: 可空

字符串模板中的可为空的var

Kotlin有一个称为字符串模板的功能。 在字符串中使用可空变量是否安全? override fun onMessageReceived(messageEvent: MessageEvent?) { Log.v(TAG, "onMessageReceived: $messageEvent") } 如果messageEvent为null ,上面的代码是否会抛出NullPointerException ?

我可以对来自Java API的平台类型执行无效安全吗?

从Kotlin版本M9中删除了“强制平台类型的无效安全”。 https://blog.jetbrains.com/kotlin/2014/10/making-platform-interop-even-smoother/ 但是,我想要使所有的Java API可以为空,除非指定为@NotNull ,这样!! 或?. 应该再次明确。 在当前版本的IntelliJ IDEA中可以吗?

Kotlin-扩展功能和平台类型?

我想将两个扩展函数添加到ResultSet ,获得一个LocalDate值。 fun ResultSet.getLocalDate(colName: String) = getDate(colName)?.toLocalDate() fun ResultSet.getLocalDate(colIndex: Int) = getDate(colIndex)?.toLocalDate() 问题是getDate()返回一个Date! ,显然我可以得到一个空错误没有?. 在toLocalDate()之前调用。 但是,那么使用这个扩展的任何人都必须使用结果作为LocalDate? 而不是LocalDate! 。 为了一致性,有没有办法维护平台类型? 让扩展函数的用户决定是否允许为空? 或者,我认为这是一个不便,而不是一个功能?

如何在Kotlin中构成可空的

假设你有2个函数返回为空,你不能编写这2个函数: fun giveInt(x: Int):Int? = x+1 fun giveInt2(x: Int):Int? = x+2 为了达到这个构图,我写了这个函数: fun all(x:Int):Int? { val y = giveInt(x) if (y != null) return giveInt2(y) else return null; } val four = all(1) if (y != null)没有明确写入if (y != null)并return null是否有可能组合2个可为null的值来获得另一个可为return null ? 在斯卡拉,我只是做一些事情: def giveInt(x: Int):Option[Int] = Some(x+1) def giveInt2(x: Int):Option[Int] = Some(x+2) def […]

Kotlin类型与泛型不匹配

我最近遇到了关于安全导航操作符( ?. )和泛型的问题。 class A<T : Any?>(private var value: T) { fun function() { value?.let { val notNull: Any = it // ^^ // Type mismatch: // Required: Any // Found: T } } } 我相信上面的代码应该没有问题运行,但会产生上面显示的错误。 我错过了什么吗?

Kotlin。 如何检查是否可以通过反射字段?

我正在开发一个代码生成器,在运行时从类中获取数据。 该发生器只能与Kotlin一起使用。 目前,我正面临着这个问题,因为我不知道如何检查该字段是否可以空。 所以主要问题是如何通过反射来实现这个检查?

在Kotlin中如何转换一个Int? 到一个Int

我在Kotlin中使用HashMap<Int, Int> ,当我离开它时,返回类型是Int? 。 我如何转换Int? 到一个Int ? 到目前为止,我尝试使用Int?.toInt() ,但似乎是返回一个Int? 。 我正在写一个Fibonacci函数,我的代码如下所示: val fibMemo : Map<Int, Int> = HashMap<Int,Int>() fun fibN(n:Int) : Int { if (n == 0 || n == 1) return 1 if (fibMemo.containsKey(n)) // Error here: Type mismatch: inferred type is Int? but Int was expected return fibMemo.get(n)?.toInt() else { val newNum : […]

用kotlin使用Glide时为空错误

我在Kotlin项目中使用滑行。 目前,Java和Kotlin是混合的。 我从下面的代码中看到崩溃报告。 (并不总是,但一些用户有这个问题)任何帮助? Glide.with(view.context) .load(HoianImage(images[position].file())) .asBitmap() .dontAnimate() .diskCacheStrategy(DiskCacheStrategy.SOURCE) .listener(object : RequestListener<HoianImage, Bitmap> { override fun onException(e: Exception, model: HoianImage, target: Target<Bitmap>, isFirstResource: Boolean): Boolean { hideProgressBar(progressBar) return false } override fun onResourceReady(resource: Bitmap, model: HoianImage, target: Target<Bitmap>, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean { hideProgressBar(progressBar) return false } }).into(imageView) hideProgressBar()方法签名如下所示。 private fun hideProgressBar(progressBar: ProgressBar?) { […]

kotlin反射检查可空类型

我怎样才能测试一个KType变量是否包含一个可为空的kotlin类型的值(eG Int?)? 我有 var type: KType 来自KProperty<*>.returnType变量,我需要检测它是否等于某些kotlin类型(Int,Long等)。 这适用于: when (type) { Int::class.defaultType -> … Long::class.defaultType -> … else -> … } 但是这只适用于不可为空的类型,所以第一个分支不匹配Int? 然而,我还没有弄清楚,我可以检测到类型是Int? 其他则显而易见,但不是那么好 type.toString().equals("kotlin.Int?")

在Kotlin中,处理可空值的惯用方式是什么,引用或转换它们

如果我有一个可空类型Xyz? ,我想引用它或将其转换为不可为空的类型Xyz 。 Kotlin这样做的习惯用法是什么? 例如,这个代码是错误的: val something: Xyz? = createPossiblyNullXyz() something.foo() // Error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Xyz?" 但是,如果我先检查null是允许的,为什么呢? val something: Xyz? = createPossiblyNullXyz() if (something != null) { something.foo() } 假如我知道它确实从不为null ,我该如何将值更改或视为非null而不要求if检查? 例如,在这里我从地图中检索一个值,我可以保证存在,并且get()的结果不为null 。 但是我有一个错误: val map = mapOf("a" to 65,"b" to […]