Kotlin String.toBoolean替换

Kotlin String.toBoolean替换,kotlin,Kotlin,因为Kotlin 1.4 String.toBoolean()已被弃用,但没有记录替换内容。 我们现在应该改用java.lang.Boolean.parseBoolean,还是有一种Kotlin方法来代替它 /** * Returns `true` if the content of this string is equal to the word "true", ignoring case, and `false` otherwise. */ @Deprecated(&

因为Kotlin 1.4 String.toBoolean()已被弃用,但没有记录替换内容。 我们现在应该改用
java.lang.Boolean.parseBoolean
,还是有一种Kotlin方法来代替它

/**
 * Returns `true` if the content of this string is equal to the word "true", ignoring case, and `false` otherwise.
 */
@Deprecated("Use Kotlin compiler 1.4 to avoid deprecation warning.")
@DeprecatedSinceKotlin(hiddenSince = "1.4")
@kotlin.internal.InlineOnly
public actual inline fun String.toBoolean(): Boolean = this.toBoolean()

/**
 * Returns `true` if this string is not `null` and its content is equal to the word "true", ignoring case, and `false` otherwise.
 */
@JvmName("toBooleanNullable")
@SinceKotlin("1.4")
@kotlin.internal.InlineOnly
public actual inline fun String?.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this)

如您在问题中所示,现在有
String?.toBoolean()
。它类似,但也可以在
null
引用上调用,在这种情况下,它将返回
false

我应该更仔细地阅读弃用。 我的Intellij使用的是Kotlin编译器1.3.72而不是1.4,因为IDE中有一个过时的Kotlin插件,但我的gradle配置是正确的,因此只有我的编辑器显示了弃用警告。
更新Intellij Kotlin插件修复了这个问题。

在我的例子中,这也是一个IDE问题,但插件已经是最新的。只需重新启动就可以解决这个问题。

IntelliJ和Android Studio使用自己的Kotlin编译器,特别是用于lint检查。因此,即使在构建脚本中已经使用了Kotlin 1.4++,也会得到“使用Kotlin编译器1.4以避免弃用警告”

您需要在设置中更改编译器首选项->语言和框架->Kotlin,更新Kotlin编译器版本


是的,所以您现在有效地使用了
String?.toBoolean()
now,正如我在回答中提到的,现在应该使用它。
// Top-level build file
buildscript {

    // IntelliJ/Android studio don't use this Kotlin version for lint check.
    ext.kotlin_version = "1.4.10"

    repositories {
        google()
        jcenter()
    }