Swift - Case语句和MetaType检查?

时间:2014-08-30 04:41:30

标签: swift

给定一个字典我需要检查以查看该值是否为Dictionary,Array或Other。 我收到以下错误:

  

不能使用Dictionary类型的Downcast模式值

// 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

1 个答案:

答案 0 :(得分:0)

您可以使用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)")
    }
}