我在 Swift 中创建了一个循环动态字典的函数,但是当我尝试检查该值是否为字典类型时,类型比较条件总是失败,事实上 XCode 会引发以下警告作为提示:
Cast from '(key: String, value: Any)' to unrelated type 'Dictionary<String, Any>' always fails.
Run Code Online (Sandbox Code Playgroud)
我不想转换任何值,我只是想检查变量值是否具有字典类型。
这是我的代码:
func readNode(node: Dictionary<String, Any>, level: Int)
{
// Print spaces
for _ in 0 ... level
{
print(" ")
}
for (key, val) in node.enumerated()
{
// The following condition is always false (here is the issue)
if val is Dictionary<String, Any> {
print("Key \(key):")
readNode(node: val, level: (level + 1) * 2)
}
else
{
print("Key \(key): \(val)")
} …Run Code Online (Sandbox Code Playgroud) swift ×1