如果要对照特定类型进行判断,可以执行以下操作:
if let stringArray = obj as? [String] {
// obj is a string array. Do something with stringArray
}
else {
// obj is not a string array
}
你可以用"as!"如果obj
不是[String]
类型,那么这将引发运行时错误
let stringArray = obj as! [String]
您还可以一次判断一个元素:
let items : [Any] = ["Hello", "World"]
for obj in items {
if let str = obj as? String {
// obj is a String. Do something with str
}
else {
// obj is not a String
}
}