快速将 [String: Any] 转换为 [String: String]

Dmi*_*lin 2 dictionary casting ios swift

如何将转换[String: Any][String: String]斯威夫特。我试过这样投射,但没有帮助:

for (key, value) in dictionary {
    dictionary[key] = value as! String
}
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 7

这是安全的方法,转换后将只包含 [String: String],非字符串 Any 将被删除:

let converted = dictionary.compactMapValues { $0 as? String }
Run Code Online (Sandbox Code Playgroud)