Kotlin 扩展函数突然需要 api 级别 24 [英] Kotlin Extension Functions suddenly require api level 24
问题描述
我刚刚注意到这个 lint 错误:
I just noticed this lint error:
调用需要 API 级别 24(当前最小值为 19) java.util.map#foreach
Call requires API Level 24 (current min is 19) java.util.map#foreach
当我在 Kotlin 的 MutableMap 上使用 forEach 扩展函数时.当我写这条线时,这并没有发生,但现在它就在那里.我在另一台机器上没有看到这个错误.
when I use the extension function forEach on a MutableMap in Kotlin. This didn't happen when I wrote the line, but its there now. And I'm not seeing this error on my other machine.
推荐答案
你使用的不是kotlin.collections.MutableMap.forEach
.
您使用的似乎是 Java 8 中的 Map.forEach
.
What you are using seems to be Map.forEach
in Java 8.
参考这篇文章:http://blog.danlew.net/2017/03/16/kotlin-puzzler-whose-line-is-it-anyways/
这似乎是一个常见的错误.
This seems to be a common mistake.
Java 8 从 Android API 级别 24 开始得到很好的支持.
Java 8 is well-supported from Android API level 24.
简而言之,不要像 map.forEach { t, u -> 那样使用
(这是使用 Java 8 API,不适用于 Android API 级别 <= 23).forEach
Log.i("tag", t + u) }
像 map.forEach { (t, u) ->Log.i("tag", t + u) }
(这是使用 Kotlin API).
(不是两个参数.只有一个参数是一对.)
For short, do not use forEach
like map.forEach { t, u -> Log.i("tag", t + u) }
(this is using Java 8 API, which does not work for Android API level <= 23).
Use it like map.forEach { (t, u) -> Log.i("tag", t + u) }
(this is using Kotlin API).
(Not two parameters. Just one parameter that is a pair.)
这篇关于Kotlin 扩展函数突然需要 api 级别 24的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!