不推荐使用Int和Int类型的参数的标识相等性

Aba*_*Aba 13 kotlin kotlin-android-extensions

只是fyi,这是我在StackOverflow上的第一个问题,我在Kotlin中真的很新.

在完成一个完全是Kotlin的项目(版本1.1.3-2)时,我看到以下代码的警告(带有好奇小伙伴的评论):

    // Code below is to handle presses of Volume up or Volume down.
    // Without this, after pressing volume buttons, the navigation bar will
    // show up and won't hide
    val decorView = window.decorView
    decorView
        .setOnSystemUiVisibilityChangeListener { visibility ->
            if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0) {
                 decorView.systemUiVisibility = flags
            }
        }
Run Code Online (Sandbox Code Playgroud)

警告是为了可见性而View.SYSTEM_UI_FLAG_FULLSCREEN === 0,它表示不推荐使用Int和Int类型的参数的标识相等性.

我应该如何更改代码?为什么它首先被弃用(出于知识的缘故)?

hol*_*ava 15

您可以使用结构相等性更改代码,如下所示:

//              use structual equality instead ---v
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
    decorView.systemUiVisibility = flags
}
Run Code Online (Sandbox Code Playgroud)

为什么不建议使用引用平等?你可以在这里看到我的答案.

另一方面,当您使用引用/身份相等时,可能会返回false,例如:

val ranged = arrayListOf(127, 127)

println(ranged[0] === ranged[1]) // true
println(ranged[0] ==  ranged[1]) // true
Run Code Online (Sandbox Code Playgroud)
val exclusive = arrayListOf(128, 128)

//                                        v--- print `false` here
println(exclusive[0] === exclusive[1]) // false
println(exclusive[0] ==  exclusive[1]) // true
Run Code Online (Sandbox Code Playgroud)