【发布时间】:2019-07-02 16:51:13
【问题描述】:
我有一些逻辑只在从 FileInputStream 中读取的对象是 Map 时运行:
private fun loadEncryptedFile(file: File, password: CharArray) {
var decrypted: ByteArray? = null
ObjectInputStream(FileInputStream(file)).use {
when (val data = it.readObject()) {
is Map<*, *> -> {
if (data.containsKey("iv") && data.containsKey("salt") && data.containsKey("encrypted")) {
val iv = data["iv"]
val salt = data["salt"]
val encrypted = data["encrypted"]
if (iv is ByteArray && salt is ByteArray && encrypted is ByteArray) {
decrypted = Encryption().decrypt(
hashMapOf("iv" to iv, "salt" to salt, "encrypted" to encrypted), password)
}
}
}
}
}
}
“数据”变量的每个实例都会给我一个“类型推断失败,应在输入类型中提及参数 K 的值”错误。
但是,用这些类型替换通配符会给我“无法检查已擦除类型的实例”错误。编译器要我在哪里告诉它 Map 中包含哪些类型?
【问题讨论】: