Kotlin String.toBoolean 替换

And*_*ndi 4 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("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)
Run Code Online (Sandbox Code Playgroud)

her*_*man 5

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