给定一个字典,我需要检查该值是否是Dictionary、Array或其他。我得到以下错误:
无法使用“类型字典”的向下转换模式值
// 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发布于 2014-08-30 04:54:09
您可以使用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)")
}
}https://stackoverflow.com/questions/25579081
复制相似问题