【发布时间】:2014-08-18 02:37:30
【问题描述】:
如何在 Swift 中匹配对象的数据类型?
喜欢:
var xyz : Any
xyz = 1;
switch xyz
{
case let x where xyz as?AnyObject[]:
println("\(x) is AnyObject Type")
case let x where xyz as?String[]:
println("\(x) is String Type")
case let x where xyz as?Int[]:
println("\(x) is Int Type")
case let x where xyz as?Double[]:
println("\(x) is Double Type")
case let x where xyz as?Float[]:
println("\(x) is Float Type")
default:println("None")
}
在这种情况下切换案例运行默认案例
【问题讨论】:
-
见[这个问题的答案][1][1]:stackoverflow.com/questions/24101450/…
-
是的,它运行
default:,因为xzy不是任何级别的数组,因此default:分支只会匹配。