Kotlin Extension Functions suddenly require api level 24

怎甘沉沦 提交于 2019-11-27 03:56:03

问题


I just noticed this lint error:

Call requires API Level 24 (current min is 19) java.util.map#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.


回答1:


What you are using is not kotlin.collections.MutableMap.forEach.

What you are using seems to be Map.forEach in Java 8.

Read this article: http://blog.danlew.net/2017/03/16/kotlin-puzzler-whose-line-is-it-anyways/

This seems to be a common mistake.

Java 8 is well-supported from Android API level 24.

For short, do not use like map.forEach { t, u -> Log.i("tag", t + u) }.
Use like map.forEach { (t, u) -> Log.i("tag", t + u) }.
(Not two parameters. Just one parameter which is a pair.)




回答2:


Map.forEach is supported in Java 8 and its support in Android started from API 24



来源:https://stackoverflow.com/questions/44751469/kotlin-extension-functions-suddenly-require-api-level-24

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!