给定一个字典,我需要检查它的值是字典、数组还是其他。 我收到以下错误:
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
最佳答案
您可以使用 switch
或 if
语句进行检查,只是您的语法不太正确。
切换:
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/