gpt4 book ai didi

Swift - Case 语句和 MetaType 检查?

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:05 24 4
gpt4 key购买 nike

给定一个字典,我需要检查它的值是字典、数组还是其他。我收到以下错误:

Downcast pattern value of type Dictionary cannot be used

// Type of dictionary to enumerate through
public typealias SourceDictionary = [String: AnyObject]
var dictionary: SourceDictionary


for (key, value) in dictionary {
switch (value) {
case value as SourceDictionary :
print("Dictionary")

case value as Array :
print("Array")

default :
print("Other")
}
}

也试过

case let someValue as SourceDictionary

最佳答案

您可以使用 switchif 语句进行检查,只是您的语法不太正确。

切换:

for (key, value) in dictionary {
switch value {
case let v as Dictionary<String, AnyObject>:
println("Dictionary in \(key)")
default:
println("other")
}
}

如果:

for (key, value) in dictionary {
if let v = value as? Dictionary<String, AnyObject> {
println("Dictionary in \(key)")
}
}

关于Swift - Case 语句和 MetaType 检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25579081/

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