我正在用 kotlin 编程,并且尝试使用 Hashmap,但是它给了我这个错误:未解析的引用,我尝试导入 java.lang.reflect.*;但这没有帮助。
这是我的代码(IDE 无法识别 T):
fun updateValues(map:HashMap<String,T>){
map.keys.forEach { key->{
when(map[key]){
is String-> sharedPrefrences.edit().putString(key,map[key])
is Integer-> sharedPrefrences.edit().putInt(key,map[key])
}
} }
sharedPrefrences.edit().apply()
}
最佳答案
您需要为您的函数指定类型参数,例如
fun <T> updateValues(map:HashMap<String,T>){ ...
关于java - 如何在 android studio 上启用泛型类型 <T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61224537/