gpt4 book ai didi

Kotlin - 针对 HashMap 的对象类型检查显示警告

转载 作者:行者123 更新时间:2023-12-02 12:14:41 27 4
gpt4 key购买 nike

我正在检查 Serializable在将其转换为 HashMap<String, String> 之前键入.但它给出了以下警告;

Cannot check for instance of erased type: kotlin.collections.HashMap /* = java.util.HashMap */



有没有办法检查是否 SerializableHashMap<String, String> 的类型那么安全施法吗?
params = if (it.getSerializable(ARG_PARAMS) is HashMap<String, String>) {
it.getSerializable(ARG_PARAMS) as HashMap<String, String>
} else null

最佳答案

实际上,您无法检查对象是否具有类型 HashMap<String, String>因为类型参数在运行时会被删除。我想建议你只使用一个安全的 Actor :

params = arguments?.getSerializable(ARG_PARAMS) as? HashMap<String, String>

Important:

It might be not clear but my code doesn't check that argument really has type HashMap<String, String>. You can pass HashMap<String, Int> and in some case, you'll get an error. In my snippet you just say to the compiler: "I know that there will be HashMap<String, String>. Just give it to me". params will be null only when there is no argument or its type differs from HashMap.

关于Kotlin - 针对 HashMap<String, String> 的对象类型检查显示警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57511146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com